我在Mongo有一个三列的收藏,比如:
{
"_id" : ObjectId("5b6db9e1d1b00b1b906c0225"),
"ip" : "127.0.0.1",
"hostFqdn" : "host-1",
"active" : true
},
{
"_id" : ObjectId("5b6db9e1d1b00b1b906c0226"),
"ip" : "127.0.0.2",
"hostFqdn" : "host-1",
"active" : false
},
{
"_id" : ObjectId("5b6db9e1d1b00b1b906c0227"),
"ip" : "127.0.0.3",
"hostFqdn" : "host-2",
"active" : true
},
{
"_id" : ObjectId("5b6db9e1d1b00b1b906c0228"),
"ip" : "127.0.0.4",
"hostFqdn" : "host-2",
"active" : true
},
{
"_id" : ObjectId("5b6db9e1d1b00b1b906c0229"),
"ip" : "127.0.0.5",
"hostFqdn" : "host-2",
"active" : false
},
{
"_id" : ObjectId("5b6f61204b0f134f6635a74b"),
"ip" : "127.0.0.6",
"hostFqdn" : "host-3",
"active" : false
}
我需要选择最多有一个活动IP的所有hostFqdn(即,对于上述数据,响应将是
host-1
和
host-3
到目前为止我得到的是:
db['hosts.status'].aggregate([[{
"$group": {
_id: "$hostFqdn",
"true": {
"$sum": {
"$cond": [{ "$lte": [{ "active": "$active" }, 1]}, 1, 0]
}
}
}
}]
但这将返回计数为0的所有三个hostsFqdn。有什么想法吗?