0

我是MongoDB和Casbah的新手,我想知道是否有人可以幫助我。Scala,casbah聚合查詢

我有以下的MongoDB的查詢工作,

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

如何將它轉換爲一個查詢卡斯巴司機能聽懂?

回答

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" -> "[email protected]") 
    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))        

} 

應該工作。