1
我有一個像這樣在我的MongoDB數據:如何算與條件的唯一項目數MongoDB中
db.x.insert({"time" : ISODate("2014-02-29T00:00:00Z"), "user" : "3" });
db.x.insert({"time" : ISODate("2014-02-29T00:00:00Z"), "user" : "2" });
...
,我有一個聚合函數來計算在一定時間跨度的活動,
db.x.aggregate([{
"$group":{
"_id":
{"$cond":[
{"$and":[
{"$gte": ["$time", ISODate("2014-02-15T00:00:00Z")]},
{"$lt" : ["$time", ISODate("2014-03-01T00:00:00Z")]}
]},
"season1",
{"$cond":[
{"$and":[
{"$gte": ["$time", ISODate("2014-03-01T00:00:00Z")]},
{"$lt" : ["$time", ISODate("2014-03-15T00:00:00Z")]}
]},
"season2",
null
]}
]},
"count": {"$sum": 1}
}},
{"$sort": { "_id": 1 }}
])
它工作正常,像這樣
{
"result" : [
{
"_id" : "season1",
"count" : 7
},
{
"_id" : "season2",
"count" : 7
}
],
"ok" : 1
}
的結果,但我真的停留在如何計算有多少ü每個時代都有哪些用戶? 有沒有辦法做到這一點,而不是每個時間間隔打電話給mongo?
你在哪裏與此。你有一個答案,一個錯誤的答案。它應該顯示如何在結果中獲得唯一用戶 –