我有一个收藏品。以下是样本文件
{
"bannerUserSessionId" : "565269c7d52ece855050b3b7bd4ae8d6f52161871394e2d312c340a086f93314",
"bannerId" : "<String> kitchen-chimneys||1488286849020|HP|PromoBanner4",
"bannerEventLabel" : "<String> kitchen-chimneys",
"bannerPage" : "HP",
"bannerWidget" : "PromoBanner4",
"bannerClickTimestamp" : "Mar 04, 2017 15:05:56 PM"
}
{
"bannerUserSessionId" : "565269c7d52ece855050b3b7bd4ae8d6f52161871394e2d312c340a086f93314",
"bannerId" : "<String> kitchen- chimneys||1488286849020|HP|PromoBanner4",
"bannerEventLabel" : "<String> kitchen-chimneys",
"bannerPage" : "HP",
"bannerWidget" : "PromoBanner4",
"bannerClickTimestamp" : "Mar 04, 2017 15:06:19 PM"
}
是否可以以这样的方式查询mongodb,以便我获得唯一的文档,因为上面两个文档是相同的,除了不同的时间戳对象?
所以我只想要一张唱片。
答案 0 :(得分:2)
您可以按键$group
并选择$first
时间戳值。
db.collection.aggregate([{
$group: {
_id: {
"bannerUserSessionId": "$bannerUserSessionId",
"bannerId": "$bannerId",
"bannerEventLabel": "$bannerEventLabel",
"bannerPage": "$bannerPage"
},
"bannerClickTimestamp": {
$first: "$bannerClickTimestamp"
}
}
}])