Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDb aggregation query using $project and $sort

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.

like image 408
Sad Mad Avatar asked Nov 26 '17 16:11

Sad Mad


1 Answers

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 }
])
like image 102
chridam Avatar answered Oct 22 '22 18:10

chridam