My MongoDb collection is as follows
{
"_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
},
}
I want to display the document that has highest sum of count of first, count of second and count of third.
In this case, the output should be -
{
"_id" : ObjectId("5a187babdbf0a03cdca0d0bd"),
"aggregationDate" : "2017-10-31",
"ipaddress" : "10.65.66.182",
"first" : {
"count" : 4
},
"second" : {
"count" : 10
},
"third" : {
"count" : 4
},
}
I only need one document as output.
db.getCollection('foo').aggregate(
{
$project: {
_id: "$ipaddress",
max: { $max: { $add: [ "$first.count", "$second.count", "$third.count"] } }
}
},
{ $sort: { refCount: -1 }}
)
I get the following exception
"errmsg" : "exception: invalid operator '$max'"
Can someone please help me with this query? Or what i am doing wrong.
You need to create a pipeline that creates the extra refCount
field to hold the total count. The first pipeline would be $addField
as it allows you to add new fields to the document. The sum is made possible with the $add
operator.
The preceding pipeline step would then be the $sort
to order the documents by the new field descending.
The final step $limit
will return a single document:
db.getCollection('foo').aggregate([
{
"$addFields": {
"refCount": {
"$add": ["$first.count", "$second.count", "$third.count"]
}
}
},
{ "$sort": { "refCount": -1 } },
{ "$limit": 1 }
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With