获得独特的计数管道

时间:2015-10-26 10:24:05

标签: mongodb aggregation-framework mongodb-.net-driver

我有mongodb文件

"_id" : ObjectId("55a6a66e072bfb0dcc1d83c1"), 
"IdUser" : NumberInt(1), 
"UserName" : "user1", 
"DeviceId" : "aaaaaaaaaaaaaaaaaaaaaaaaaa", 
"RegTime" :  ISODate("2015-07-15T18:20:02.896+0000")


"_id" : ObjectId("55a6a66e072bfb0dcc1d83c2"), 
"IdUser" : NumberInt(2), 
"UserName" : "user2", 
"DeviceId" : "aaaaaaaaaaaaaaaaaaaaaaaaaa", 
"RegTime" :  ISODate("2015-07-15T18:20:02.896+0000")


"_id" : ObjectId("55a6a66e072bfb0dcc1d83c3"), 
"IdUser" : NumberInt(3), 
"UserName" : "user3", 
"DeviceId" : "aaaaaaaaaaaaaaaaaaaaaaaaaa", 
"RegTime" :  ISODate("2015-07-15T18:20:02.896+0000")


"_id" : ObjectId("55a6a66e072bfb0dcc1d83c4"), 
"IdUser" : NumberInt(4), 
"UserName" : "user4", 
"DeviceId" : "aaaaaaaaaaaaaaaaaaaaaaaaaa", 
"RegTime" :  ISODate("2015-07-15:20:02.896+0000")


"_id" : ObjectId("55a6a66e072bfb0dcc1d83c5"), 
"IdUser" : NumberInt(5), 
"UserName" : "user5", 
"DeviceId" : "bbbbbbbbbbbbbbbbbbbbbbbbbb", 
"RegTime" :  ISODate("2015-07-15T18:20:02.896+0000")


"_id" : ObjectId("55a6a66e072bfb0dcc1d83c6"), 
"IdUser" : NumberInt(6), 
"UserName" : "user6", 
"DeviceId" : "cccccccccccccccccccccccccc", 
"RegTime" :  ISODate("2015-07-15T18:20:02.896+0000")

我希望结果计数设备ID在白天有所不同 2015-07-15 = CountDeviceDistinct = 3 {aaaaaaaaaaaaaaa,bbbbbbbbbbbbbbbbb,cccccccccccccc}

1 个答案:

答案 0 :(得分:-1)

我认为您应该按日期分组并在字段中收集设备ID,如下所示:

db.collection.aggregate(
   [{ $group : {
           _id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } }, DeviceIds: { $addToSet: "$DeviceId" }
    }}
   ]
)