Skip to content

Instantly share code, notes, and snippets.

@damianmcdonald
Created July 12, 2015 09:16
Show Gist options
  • Save damianmcdonald/a5aeb4c7ba6db1d7a14f to your computer and use it in GitHub Desktop.
Save damianmcdonald/a5aeb4c7ba6db1d7a14f to your computer and use it in GitHub Desktop.
MongoDB commands to Scala Casbah examples
import com.mongodb.casbah.Imports._
import com.mongodb.casbah.commons.MongoDBObject
class MongoDBToCasbahExamples {
/* The BSON structure that will be used for the examples
{
"userId": "134256",
"currencyFrom": "EUR",
"currencyTo": "GBP",
"amountSell": 1000,
"amountBuy": 747.1,
"rate": 0.7471,
"originatingCountry": "FR"
}
*/
/* The MongoDB connection */
private val collection = {
val connection = MongoClient("some-host", 12345)
connection("some-database")("some-collection")
}
/**
* Persists a trade object to MongoDB
*
* ==Corresponds to the following MongoDB command==
*
* db.trades.insert({
* "userId": "134256",
* "currencyFrom": "EUR",
* "currencyTo": "GBP",
* "amountSell": 1000,
* "amountBuy": 747.1,
* "rate": 0.7471,
* "originatingCountry": "FR"
* })
*
*/
def persistTrade = {
// create the MongoDBObject
val dbObject = {
val builder1 = MongoDBObject.newBuilder
builder1 += "userId" -> "lukeskywalker"
builder1 += "currencyFrom" -> "EUR"
builder1 += "currencyTo" -> "USD"
builder1 += "amountSell" -> 1245.67
builder1 += "amountBuy" -> 978.10
builder1 += "rate" -> 0.7852
builder1 += "originatingCountry" -> "AU"
builder1.result
}
// insert the DBObject to MongoDB
collection.save(dbObject)
}
/**
* Retrieves the top 10 countries by trade volume.
*
* ==Corresponds to the following MongoDB query==
*
* db.trades.aggregate( [
* { $group : { _id : "$originatingCountry", volume: { $sum: 1 } } },
* { "$project":{"_id":0, "country":"$_id", "volume": 1 } },
* { $sort: { count: -1 } },
* { $limit:10 }
* ] )
*
* @return List[DBObject] the top 10 countries by trade volume
*/
def getCountriesByTradeVolume: List[DBObject] = {
collection.aggregate(
List(
// the $group query
MongoDBObject("$group" -> MongoDBObject(
"_id" -> "$originatingCountry",
"volume" -> MongoDBObject("$sum" -> 1)
)),
// the $project query
MongoDBObject("$project" ->
MongoDBObject("_id" -> 0, "country" -> "$_id", "volume" -> 1)),
// the $sort query
MongoDBObject("$sort" -> MongoDBObject("volume" -> -1)),
// the $limit query
MongoDBObject("$limit" -> 10)
),
// the aggregation
AggregationOptions(AggregationOptions.CURSOR)
).toList
}
/**
* Retrieves the top 10 currency pairs by volume sold.
*
* ==Corresponds to the following MongoDB query==
*
* db.trades.aggregate([
* { "$group": { "_id": { "currencyFrom": "$currencyFrom", "currencyTo": "$currencyTo" }, "volume": { "$sum": 1 } }},
* { "$project":{"_id":0, "currencyPair":"$_id.currencyFrom", "currencyTo":"$_id.currencyTo", "volume": 1 } },
* { $sort: { volume: -1 } },
* { $limit:10 }
* ])
*
* @return List[DBObject] the top 10 currency pairs by volume sold
*/
def getCurrencyPairsByVolume: List[DBObject] = {
collection.aggregate(
List(
// the $group query
MongoDBObject("$group" -> MongoDBObject(
// definition of composite _id
"_id" -> MongoDBObject("currencyFrom" -> "$currencyFrom", "currencyTo" -> "$currencyTo"),
"volume" -> MongoDBObject("$sum" -> 1)
)),
// the $project query
MongoDBObject("$project" ->
MongoDBObject("_id" -> 0, "currencyFrom" -> "$_id.currencyFrom", "currencyTo" -> "$_id.currencyTo", "volume" -> 1)),
// the $sort query
MongoDBObject("$sort" -> MongoDBObject("volume" -> -1)),
// the $limit query
MongoDBObject("$limit" -> 10)
),
// the aggregation
AggregationOptions(AggregationOptions.CURSOR)
).toList
}
/**
* Finds the latest 10 trades
*
* ==Corresponds to the following MongoDB query==
*
* db.trades.find(
* null,
* {_id:0, currencyFrom:1, currencyTo:1, amountSell:1, amountBuy:1, rate:1, originatingCountry:1 }
* )
* .sort( {originatingCountry:-1} )
* .limit(10)
*
* @return List[DBObject] the latest 10 trades
*/
def getLatestTrades: List[DBObject] = {
collection.find(
// equivalent to a null entry in a MongoDB command
MongoDBObject.empty,
// the list of fields to display from the retrieved objects
MongoDBObject(
"_id" -> 0,
"currencyFrom" -> 1,
"currencyTo" -> 1,
"amountSell" -> 1,
"amountBuy" -> 1,
"rate" -> 1,
"originatingCountry" -> 1
)
)
// the sort query
.sort(MongoDBObject("originatingCountry" -> -1))
// the limit query
.limit(10)
.toList
}
/**
* Retrieves the distinct countries from which a trade has been placed.
*
* ==Corresponds to the following MongoDB query==
*
* db.trades.distinct("originatingCountry")
*
* @return List[String] the countries codes
*/
def getCountryCodes: List[String] = {
collection.distinct("originatingCountry").map(e => e.toString).sortWith(_ < _).toList
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment