0

I am working on mongodb for my current project my collection as follows

{
"_id" : ObjectId("5a3a567a8fb6e20f67cb10f7"),
"player_id" : "5a26453db767c01262eddc4e",
"quiz_type" : "Single",
"created_date" : ISODate("2017-12-20T12:24:26Z"),
"questions_answered" : [
    {
        "question_id" : ObjectId("5a3a0bfc2d53f131068b4567"),
        "player_selection_status" : "Wrong",
        "time_taken" : 10,
        "points_gained" : 0,
        "like" : 1,
        "answered_date" : "2017-12-20T17:54:30+05:30"
    },
    {
        "question_id" : ObjectId("5a3a0bfc2d53f131068b4568"),
        "player_selection_status" : "Correct",
        "time_taken" : 10,
        "points_gained" : 5,
        "like" : 1,
        "answered_date" : "2017-12-20T17:54:32+05:30"
    },
    {
        "question_id" : ObjectId("5a3a0bfc2d53f131068b4569"),
        "player_selection_status" : "Correct",
        "time_taken" : 10,
        "points_gained" : 5,
        "like" : 1,
        "answered_date" : "2017-12-20T17:54:34+05:30"
    },
    {
        "question_id" : ObjectId("5a3a0bfc2d53f131068b456a"),
        "player_selection_status" : "Wrong",
        "time_taken" : 10,
        "points_gained" : 0,
        "like" : 1,
        "answered_date" : "2017-12-20T17:54:35+05:30"
    },
    {
        "question_id" : ObjectId("5a3a0bfc2d53f131068b456c"),
        "player_selection_status" : "Correct",
        "time_taken" : 10,
        "points_gained" : 5,
        "like" : 1,
        "answered_date" : "2017-12-20T17:54:37+05:30"
    }
],
"__v" : 0

}

I need get data of points_gained : 5 only and my query is

db.player_quiz.find({player_id: "5a26453db767c01262eddc4e", 'questions_answered.points_gained': 5}).pretty()

using above query i am getting all results.. i need only records having questions_answered.points_gained: 5 only

please help me with a solution.

kishan kurivella
  • 589
  • 4
  • 7
  • 19
  • 1
    Possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – s7vr Dec 20 '17 at 18:15

1 Answers1

1
db.player_quiz.aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $unwind: {
                "path": '$questions_answered'
            }
        },

        // Stage 2
        {
            $match: {
                'questions_answered.points_gained': 5
            }
        },

        // Stage 3
        {
            $group: {
                _id: '$_id',
                questions_answered: {
                    $addToSet: '$questions_answered'
                },
                doc: {
                    $first: '$$ROOT'
                }
            }
        },

        // Stage 4
        {
            $project: {
                questions_answered: 1,
                "player_id": '$doc.player_id',
                'quiz_type': '$doc.quiz_type',
                'created_date': '$doc.created_date'
            }
        },

    ]



);
Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26
  • Hi @Rubin Porwal, How to find questions_answered of my above document of collection db.player_quiz.aggregate({ $unwind:"$questions_answered"},{$group:{_id:ObjectId("5a3a567a8fb6e20f67cb10f7"),count:{$sum:1}}}) i am getting wrong result like count getting 294, but in questions_answerd have only count 5 – kishan kurivella Dec 27 '17 at 07:19