这是我的收藏
[
{
_id: "585be0da13385513689f704a",
site: "NBT",
url: "m.nbt.com",
label: "NBT Home",
platform: "mobile",
speed: 61,
date: "2016-12-22T14:18:57.975Z"
},
{
_id: "585be0da13385513689f704b",
site: "NBT",
url: "nbt.com",
label: "NBT Home",
platform: "desktop",
speed: 75,
date: "2016-12-22T14:15:57.975Z"
},
{
_id: "585be0da13385513689f704c",
site: "MT",
url: "m.mt.com",
label: "MT Home",
platform: "mobile",
speed: 40,
date: "2016-12-22T14:01:57.975Z"
},
{
_id: "585be0da13385513689f704d",
site: "NBT",
url: "m.nbt.com",
label: "NBT Home",
platform: "mobile",
speed: 90,
date: "2016-12-22T12:18:57.975Z"
}
]
预期产量:
[
{
_id: "585be0da13385513689f704b",
site: "NBT",
url: "nbt.com",
label: "NBT Home",
platform: "desktop",
speed: 75,
date: "2016-12-22T14:15:57.975Z"
},
{
_id: "585be0da13385513689f704c",
site: "MT",
url: "m.mt.com",
label: "MT Home",
platform: "mobile",
speed: 40,
date: "2016-12-22T14:01:57.975Z"
},
{
_id: "585be0da13385513689f704d",
site: "NBT",
url: "m.nbt.com",
label: "NBT Home",
platform: "mobile",
speed: 90,
date: "2016-12-22T12:18:57.975Z"
}
]
基本上,我想先匹配不同的(标签+平台)组合文档。
如您所见,label- NBT Home存在于3个文档中,但是我只想检索label + platform的唯一组合。因此,我期望移动版NBT Home和桌面版NBT Home。
请帮忙。
试试这个 :
db.collection.aggregate([
{
$group:{
_id:{
label:"$label",
platform:"$platform"
},
site:{
$first:"$site"
},
url:{
$first:"$url"
},
speed:{
$first:"$speed"
}
}
}
])
输出:
{
"_id" : {
"label" : "MT Home",
"platform" : "mobile"
},
"site" : "MT",
"url" : "m.mt.com",
"speed" : 40
}
{
"_id" : {
"label" : "NBT Home",
"platform" : "desktop"
},
"site" : "NBT",
"url" : "nbt.com",
"speed" : 75
}
{
"_id" : {
"label" : "NBT Home",
"platform" : "mobile"
},
"site" : "NBT",
"url" : "m.nbt.com",
"speed" : 61
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句