我是MongoDB和Casbah的新手,我想知道有没有人能帮我。
我有以下可以工作的mongoDB查询,
db.getCollection('holidayRequests').aggregate
(
[
{ $match: { $and: [ { email: "leeroy.jenkins@company.com" } , { status: "APPROVED" } ] }},
{
$group:
{
_id: { email: "$email" },
totalAmount: { $sum: "$daysTaken" }
}
}
]
);如何将其转换为casbah驱动程序可以理解的查询?
发布于 2016-10-04 21:35:51
大概是这样的:
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))
}应该行得通。
https://stackoverflow.com/questions/39830497
复制相似问题