Scala,casbah聚合查询

时间:2016-10-03 11:27:07

标签: mongodb scala aggregation-framework casbah mongodb-aggregation

我是MongoDB和Casbah的新手,我想知道是否有人可以帮助我。

我有以下可用的mongoDB查询,

db.getCollection('holidayRequests').aggregate
(
   [
     {  $match: {  $and: [ { email: "leeroy.jenkins@company.com" } , { status: "APPROVED" } ] }},
      {
          $group:
      {
           _id: {  email: "$email" },
           totalAmount: { $sum: "$daysTaken" }
      }
     }
   ]
);

如何将此转换为casbah驱动程序可以理解的查询?

1 个答案:

答案 0 :(得分:0)

类似的东西:

import com.mongodb.casbah.commons.MongoDBObject
import com.mongodb.casbah.commons.MongoDBList
import com.mongodb.casbah.MongoClient

object testCasbah {

  val mongoClient = MongoClient() /* database connections parameters */
  val db = mongoClient("databaseName")

  val email  =  MongoDBObject("email" -> "leeroy.jenkins@company.com")
  val status =  MongoDBObject("status" ->  "APPROVED" )

  val and    = MongoDBObject("$and" -> List(email, status))

  val pipeline = MongoDBList(MongoDBObject("$match" -> and)) ++
                 MongoDBList(MongoDBObject("$group" -> 
                    MongoDBObject("_id" -> MongoDBObject("email" -> "$email"), 
                                  "totalAmount" ->  MongoDBObject("$sum" -> "$daysTaken"))))

  db.command(MongoDBObject("aggregate" -> "holidayRequests", "pipeline" -> pipeline))                            

}

应该有用。