2
我試圖在ReactiveMongo中實現一個聚合方法,但是我有點卡住了。將mongodb聚合函數轉換爲ReactiveMongo和scala
我有以下數據集:
{
"_id" : ObjectId("522891aa40ef0b5d11cb9232"),
"created" : 1378390442167,
"origin" : 2,
"originIpAddress" : "",
"rating" : 3,
"remindersSent" : 1,
"status" : 4,
"text" : "",
"updated" : 1378563426223,
"userInfo" : {
"firstName" : "Person",
"lastName" : "Person",
"email" : "[email protected]",
"fbPublish" : false
},
"venueInfo" : {
"isAgent" : false,
"name" : "Company",
"id" : 1234
}
},
{
"_id" : ObjectId("522891aa40ef0b5d11cb9233"),
"created" : 1378390442167,
"origin" : 2,
"originIpAddress" : "",
"rating" : 3,
"remindersSent" : 1,
"status" : 4,
"text" : "",
"updated" : 1378563426223,
"userInfo" : {
"firstName" : "Person2",
"lastName" : "Person2",
"email" : "[email protected]",
"fbPublish" : false
},
"venueInfo" : {
"isAgent" : false,
"name" : "Company2",
"id" : 4321
}
},
{
"_id" : ObjectId("522891aa40ef0b5d11cb9234"),
"created" : 1378390442167,
"origin" : 2,
"originIpAddress" : "",
"rating" : 3,
"remindersSent" : 1,
"status" : 4,
"text" : "",
"updated" : 1378563426223,
"userInfo" : {
"firstName" : "Person3",
"lastName" : "Person3",
"email" : "[email protected]",
"fbPublish" : false
},
"venueInfo" : {
"isAgent" : false,
"name" : "Company",
"id" : 1234
}
}
以下聚合函數:
db.reviews.aggregate(
{$match:{status:{"$ne":1}}},
{$group: { _id: "$venueInfo.id", total:{"$sum":1}}}
)
給我:
{
"result" : [
{
"_id" : 1234,
"total" : 2
},
{
"_id" : 4321,
"total" : 1
}
]
}
我試圖在ReactiveMongo來實現這一點:
def aggregate() = {
val command = Aggregate(collection.name, Seq(
GroupField("venueInfo.id")("total" -> SumValue(1)),
Match(BSONDocument("status" -> 1))
))
val result = collection.db.command(command)
result.map { value => {
println(s"got value $value")
}
}
這一點讓我:
got value Stream(BSONDocument(<non-empty>), ?)
正如你看到的,我收到了流回來。所以我的問題是:如何以正確的方式處理這個流,以便我可以使用這些值並在視圖中稍後顯示它們?
我想在你的代碼的匹配是不正確的,在樣本數據存在與狀態沒有行= 1 匹配(BSONDocument(「狀態」 - > BSONDocument(「$ NE」 - > 1)))也許是正確的 – barczajozsef