1

I'm using mongo for the first time. I'm trying to aggregate some documents in a collection using the query below. Instead the query returns an object with a key "result" that contains an array of all the documents that fit with $match.

Below is the query.

db.events_2015_04_10.aggregate([
                {$group:{
                     _id: "$uid",
                     count: {$sum: 1},
                    },
                    $match : {promo:"bc40100abc8d4eb6a0c68f81f4a756c7", evt:"login"}
                }
            ]
        );

Below is a sample document in the collection:

{
    "_id" : ObjectId("552712c3f92ea17426000ace"),
    "product" : "Mobile Safari",
    "venue_id" : NumberLong(71540),
    "uid" : "dd542fea6b4443469ff7bf1f56472eac",
    "ag" : 0,
    "promo" : "bc40100abc8d4eb6a0c68f81f4a756c7",
    "promo_f" : NumberLong(1),
    "brand" : NumberLong(17),
    "venue" : "ovation_2480",
    "lt" : 0,
    "ts" : ISODate("2015-04-10T00:01:07.734Z"),
    "evt" : "login",
    "mac" : "00:00:00:00:00:00",
    "__ns__" : "wifipromo",
    "pvdr" : NumberLong(42),
    "os" : "iPhone",
    "cmpgn" : "fc6de34aef8b4f57af0b8fda98d8c530",
    "ip" : "192.119.43.250",
    "lng" : 0,
    "product_ver" : "8"
}

I'm trying to get it all grouped by uid's with the total sum of each group... What is the correct way to achieve this?

1 Answer 1

1

Try the following aggregation framework which has the $match pipeline stage first and then the $group pipeline later:

db.events_2015_04_10.aggregate([
    {
        $match: {
            promo: "bc40100abc8d4eb6a0c68f81f4a756c7", 
            evt: "login"
        }
    },
    {
        $group: {
            _id: "$uid",
            count: {
                $sum: 1
            }
        }        
    }
])
Sign up to request clarification or add additional context in comments.

Comments

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.