我的MongoDb集合如下:
我只需要一个文档作为输出。
我遇到了以下异常。
{
"_id" : ObjectId("5a187babdbf0a03cdca0d0bc"),
"aggregationDate" : "2017-10-31",
"ipaddress" : "10.65.66.184",
"first" : {
"count" : 3
},
"second" : {
"count" : 2
},
"third" : {
"count" : 3
},
}
{
"_id" : ObjectId("5a187babdbf0a03cdca0d0bd"),
"aggregationDate" : "2017-10-31",
"ipaddress" : "10.65.66.182",
"first" : {
"count" : 4
},
"second" : {
"count" : 10
},
"third" : {
"count" : 4
},
}
{
"_id" : ObjectId("5a187babdbf0a03cdca0d0be"),
"aggregationDate" : "2017-10-31",
"ipaddress" : "10.65.66.189",
"first" : {
"count" : 3
},
"second" : {
"count" : 1
},
"third" : {
"count" : 12
},
}
我希望显示拥有第一、第二和第三计数之和最高的文档。
在这种情况下,输出应该是 -
{
"_id" : ObjectId("5a187babdbf0a03cdca0d0bd"),
"aggregationDate" : "2017-10-31",
"ipaddress" : "10.65.66.182",
"first" : {
"count" : 4
},
"second" : {
"count" : 10
},
"third" : {
"count" : 4
},
}
我只需要一个文档作为输出。
db.getCollection('foo').aggregate(
{
$project: {
_id: "$ipaddress",
max: { $max: { $add: [ "$first.count", "$second.count", "$third.count"] } }
}
},
{ $sort: { refCount: -1 }}
)
我遇到了以下异常。
"errmsg" : "exception: invalid operator '$max'"
有人可以帮我解决这个查询吗?或者我做错了什么。