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?
你在哪里与此。你有一个答案,一个错误的答案。它应该显示如何在结果中获得唯一用户 –