【发布时间】:2018-05-09 22:41:32
【问题描述】:
我的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'"
有人可以帮我解决这个问题吗?或者我做错了什么。
【问题讨论】:
标签: mongodb aggregation