Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. import com.mongodb.casbah.Imports._
  2. import com.mongodb.casbah.commons.MongoDBObject
  3.  
  4. class MongoDBToCasbahExamples {
  5.  
  6. /* The BSON structure that will be used for the examples
  7. {
  8. "userId": "134256",
  9. "currencyFrom": "EUR",
  10. "currencyTo": "GBP",
  11. "amountSell": 1000,
  12. "amountBuy": 747.1,
  13. "rate": 0.7471,
  14. "originatingCountry": "FR"
  15. }
  16. */
  17.  
  18. /* The MongoDB connection */
  19. private val collection = {
  20. val connection = MongoClient("some-host", 12345)
  21. connection("some-database")("some-collection")
  22. }
  23.  
  24. /**
  25. * Persists a trade object to MongoDB
  26. *
  27. * ==Corresponds to the following MongoDB command==
  28. *
  29. * db.trades.insert({
  30. * "userId": "134256",
  31. * "currencyFrom": "EUR",
  32. * "currencyTo": "GBP",
  33. * "amountSell": 1000,
  34. * "amountBuy": 747.1,
  35. * "rate": 0.7471,
  36. * "originatingCountry": "FR"
  37. * })
  38. *
  39. */
  40. def persistTrade = {
  41. // create the MongoDBObject
  42. val dbObject = {
  43. val builder1 = MongoDBObject.newBuilder
  44. builder1 += "userId" -> "lukeskywalker"
  45. builder1 += "currencyFrom" -> "EUR"
  46. builder1 += "currencyTo" -> "USD"
  47. builder1 += "amountSell" -> 1245.67
  48. builder1 += "amountBuy" -> 978.10
  49. builder1 += "rate" -> 0.7852
  50. builder1 += "originatingCountry" -> "AU"
  51. builder1.result
  52. }
  53.  
  54. // insert the DBObject to MongoDB
  55. collection.save(dbObject)
  56. }
  57.  
  58. /**
  59. * Retrieves the top 10 countries by trade volume.
  60. *
  61. * ==Corresponds to the following MongoDB query==
  62. *
  63. * db.trades.aggregate( [
  64. * { $group : { _id : "$originatingCountry", volume: { $sum: 1 } } },
  65. * { "$project":{"_id":0, "country":"$_id", "volume": 1 } },
  66. * { $sort: { count: -1 } },
  67. * { $limit:10 }
  68. * ] )
  69. *
  70. * @return List[DBObject] the top 10 countries by trade volume
  71. */
  72. def getCountriesByTradeVolume: List[DBObject] = {
  73. collection.aggregate(
  74. List(
  75. // the $group query
  76. MongoDBObject("$group" -> MongoDBObject(
  77. "_id" -> "$originatingCountry",
  78. "volume" -> MongoDBObject("$sum" -> 1)
  79. )),
  80. // the $project query
  81. MongoDBObject("$project" ->
  82. MongoDBObject("_id" -> 0, "country" -> "$_id", "volume" -> 1)),
  83. // the $sort query
  84. MongoDBObject("$sort" -> MongoDBObject("volume" -> -1)),
  85. // the $limit query
  86. MongoDBObject("$limit" -> 10)
  87. ),
  88. // the aggregation
  89. AggregationOptions(AggregationOptions.CURSOR)
  90. ).toList
  91. }
  92.  
  93. /**
  94. * Retrieves the top 10 currency pairs by volume sold.
  95. *
  96. * ==Corresponds to the following MongoDB query==
  97. *
  98. * db.trades.aggregate([
  99. * { "$group": { "_id": { "currencyFrom": "$currencyFrom", "currencyTo": "$currencyTo" }, "volume": { "$sum": 1 } }},
  100. * { "$project":{"_id":0, "currencyPair":"$_id.currencyFrom", "currencyTo":"$_id.currencyTo", "volume": 1 } },
  101. * { $sort: { volume: -1 } },
  102. * { $limit:10 }
  103. * ])
  104. *
  105. * @return List[DBObject] the top 10 currency pairs by volume sold
  106. */
  107. def getCurrencyPairsByVolume: List[DBObject] = {
  108. collection.aggregate(
  109. List(
  110. // the $group query
  111. MongoDBObject("$group" -> MongoDBObject(
  112. // definition of composite _id
  113. "_id" -> MongoDBObject("currencyFrom" -> "$currencyFrom", "currencyTo" -> "$currencyTo"),
  114. "volume" -> MongoDBObject("$sum" -> 1)
  115. )),
  116. // the $project query
  117. MongoDBObject("$project" ->
  118. MongoDBObject("_id" -> 0, "currencyFrom" -> "$_id.currencyFrom", "currencyTo" -> "$_id.currencyTo", "volume" -> 1)),
  119. // the $sort query
  120. MongoDBObject("$sort" -> MongoDBObject("volume" -> -1)),
  121. // the $limit query
  122. MongoDBObject("$limit" -> 10)
  123. ),
  124. // the aggregation
  125. AggregationOptions(AggregationOptions.CURSOR)
  126. ).toList
  127. }
  128.  
  129. /**
  130. * Finds the latest 10 trades
  131. *
  132. * ==Corresponds to the following MongoDB query==
  133. *
  134. * db.trades.find(
  135. * null,
  136. * {_id:0, currencyFrom:1, currencyTo:1, amountSell:1, amountBuy:1, rate:1, originatingCountry:1 }
  137. * )
  138. * .sort( {originatingCountry:-1} )
  139. * .limit(10)
  140. *
  141. * @return List[DBObject] the latest 10 trades
  142. */
  143. def getLatestTrades: List[DBObject] = {
  144. collection.find(
  145. // equivalent to a null entry in a MongoDB command
  146. MongoDBObject.empty,
  147. // the list of fields to display from the retrieved objects
  148. MongoDBObject(
  149. "_id" -> 0,
  150. "currencyFrom" -> 1,
  151. "currencyTo" -> 1,
  152. "amountSell" -> 1,
  153. "amountBuy" -> 1,
  154. "rate" -> 1,
  155. "originatingCountry" -> 1
  156. )
  157. )
  158. // the sort query
  159. .sort(MongoDBObject("originatingCountry" -> -1))
  160. // the limit query
  161. .limit(10)
  162. .toList
  163. }
  164.  
  165. /**
  166. * Retrieves the distinct countries from which a trade has been placed.
  167. *
  168. * ==Corresponds to the following MongoDB query==
  169. *
  170. * db.trades.distinct("originatingCountry")
  171. *
  172. * @return List[String] the countries codes
  173. */
  174. def getCountryCodes: List[String] = {
  175. collection.distinct("originatingCountry").map(e => e.toString).sortWith(_ < _).toList
  176. }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement