kishan kurivella
kishan kurivella

Reputation: 599

How to get collection sub document count in Mongo

At present I am working on Mongo for current project, please help me with solution enter image description here

I need questions_answered count of this document.

I tried with following query but I am not getting correct answer

db.player_quiz.aggregate({ $unwind:"$questions_answered"},{$group:{_id:ObjectId("5a3a56‌​7a8fb6e20f67cb10f7")‌​,count:{$sum:1}}})

Correct answer is 2 but I am getting 15.. please help me with solution.

Upvotes: 0

Views: 642

Answers (1)

Avij
Avij

Reputation: 694

I have a collection which looks like below -

   {
        "_id" : ObjectId("5a4c68c8ea76fd79ec70c740"),
        "address" : {
                "building" : "1007",
                "coord" : [
                        -73.856077,
                        40.848447
                ],
                "street" : "Morris Park Ave",
                "zipcode" : "10462"
        },
        "borough" : "Bronx",
        "cuisine" : "Bakery",
        "grades" : [
                {
                        "date" : ISODate("2014-03-03T00:00:00Z"),
                        "grade" : "A",
                        "score" : 2
                },
                {
                        "date" : ISODate("2013-09-11T00:00:00Z"),
                        "grade" : "A",
                        "score" : 6
                },
                {
                        "date" : ISODate("2013-01-24T00:00:00Z"),
                        "grade" : "A",
                        "score" : 10
                },
                {
                        "date" : ISODate("2011-11-23T00:00:00Z"),
                        "grade" : "A",
                        "score" : 9
                },
                {
                        "date" : ISODate("2011-03-10T00:00:00Z"),
                        "grade" : "B",
                        "score" : 14
                }
        ],
        "name" : "Morris Park Bake Shop",
        "restaurant_id" : "30075445"
}

To find the size of grades i use it like -

db.location.aggregate([{$match:{_id:ObjectId("5a2f66c7a3811ba54bbaaabe")}},{$project:{'ans':{$size:'*$grades*'}}}])

In your case replace $grades with $questions_answered. Hope this helps.

As you asked about getting sum - Below query return sum as well as count

db.location.aggregate([{
            $match: {
                _id: ObjectId("5a4c68c8ea76fd79ec70c740")//REPLACE THIS
            }
        }, {
            $unwind: '$grades' //REPLACE THIS
        }, {
            $group: {
                _id: "$_id",
                'sum': {
                    $sum: '$grades.score'//REPLACE THIS
                },
                'doc': {
                    $push: '$grades' //REPLACE THIS
                }
            }
        }, {
            $project: {
                'sum': '$sum',
                'size': {
                    $size: '$doc'
                }
            }
        }
    ])

Result

{ "_id" : ObjectId("5a4c68c8ea76fd79ec70c740"), "sum" : 41, "size" : 5 }

Upvotes: 1

Related Questions