5

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.

1 Answer 1

9

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 }
])
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks buddy, you explained the answer wonderfully as well. Also, in this case, I only get output as refCount. Is there any way to display the other fields such as "_id" : ObjectId("5a187babdbf0a03cdca0d0bd"), "aggregationDate" : "2017-10-31", "ipaddress" : "10.65.66.182"?
Also, more importantly, I have an additional question. I have one document, per ipaddress, per day. How do I find the ipaddress most active over all the days? The above query gives me the highest for one particular day.. But how do i compute a highest based on, value of $add over all the days?
Any solution ??
@Charlyberthet Solution to which question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.