1

My document contains nearly seven levels of the document in MongoDB, now I need to write a query to check all conditions like, subject=java, topic=oops, level=l1, complexity=easy, questionType=mcq based on this requirement display all questions in the collection. Any one please help me out, Thanks in advance.

{
    "_id" : ObjectId("59f71b4d0bec333e1707a8d3"),
    "_class" : "com.wipro.domain.QuestionBank",
    "subjectLists" : [ 
        {
            "subject" : "java",
            "topicList" : [ 
                {
                    "topic" : "oops",
                    "levelList" : [ 
                        {
                            "level" : "l1",
                            "complexityList" : [ 
                                {
                                    "complexity" : "easy",
                                    "questionTypeList" : [ 
                                        {
                                            "questionType" : "mcq",
                                            "questionList" : [ 
                                                {
                                                    "_id" : "2",
                                                    "question" : "2st question",
                                                    "options" : [ 
                                                        {
                                                            "a" : "1",
                                                            "b" : "2",
                                                            "c" : "3",
                                                            "d" : "4"
                                                        }
                                                    ],
                                                    "correctAnswer" : "b",
                                                    "marksAlloted" : "1"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}


{
    "_id" : ObjectId("59f71b700bec333e1707a8d4"),
    "_class" : "com.wipro.domain.QuestionBank",
    "subjectLists" : [ 
        {
            "subject" : "java",
            "topicList" : [ 
                {
                    "topic" : "threds",
                    "levelList" : [ 
                        {
                            "level" : "l3",
                            "complexityList" : [ 
                                {
                                    "complexity" : "hard",
                                    "questionTypeList" : [ 
                                        {
                                            "questionType" : "mcq",
                                            "questionList" : [ 
                                                {
                                                    "_id" : "3",
                                                    "question" : "3rd question",
                                                    "options" : [ 
                                                        {
                                                            "a" : "1",
                                                            "b" : "2",
                                                            "c" : "3",
                                                            "d" : "4"
                                                        }
                                                    ],
                                                    "correctAnswer" : "b",
                                                    "marksAlloted" : "1"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}
Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44
Roy
  • 880
  • 7
  • 21
  • 39
  • What output/query are you expecting? – Atish Nov 04 '17 at 12:36
  • Query by what do you need? You may simply call `db.collection.find({"subjectLists.subject":"java"},{"subjectLists.topicList.levelList.complexityList.questionTypeList.questionList":1})` that will return all questions that match `subject = java` – Larry Foobar Nov 04 '17 at 12:44
  • subject=java,topic=oops,level=l1,complexity=easy,questionType=mcq based on this requirement display all questions in the collection. – Roy Nov 04 '17 at 16:11

2 Answers2

0

Following query with projection lists only questions:

Note: Pass your query below:

db.questions.findOne({}, {'subjectLists.topicList.levelList.complexityList.questionTypeList.questionList':1})
Atish
  • 4,277
  • 2
  • 24
  • 32
0

You can try this script.

db.getCollection('document').aggregate([
{
    $match:{
        $and: [
            {"subjectLists.subject": "java"}
            ,{"subjectLists.topicList.topic": "oops"}
            ,{"subjectLists.topicList.levelList.level": "l1"}
            ,{"subjectLists.topicList.levelList.complexityList.complexity": "easy"}
            ,{"subjectLists.topicList.levelList.complexityList.questionTypeList.questionType": "mcq"}
        ]       
    }    
},
{
    $unwind: "$subjectLists"
}
,{
    $unwind: "$subjectLists.topicList"
}
,{
    $unwind: "$subjectLists.topicList.levelList"
}
,{
    $unwind: "$subjectLists.topicList.levelList.complexityList"
}
,{
    $unwind: "$subjectLists.topicList.levelList.complexityList.questionTypeList"
}
,{
    $unwind: "$subjectLists.topicList.levelList.complexityList.questionTypeList.questionList"
},
{
    $project: {
        "questionList":  "$subjectLists.topicList.levelList.complexityList.questionTypeList.questionList"
    }   
}
,
{
        $replaceRoot: { newRoot: "$questionList" }
}
])

Result:

{
    "_id" : "2",
    "question" : "2st question",
    "options" : [ 
        {
            "a" : "1",
            "b" : "2",
            "c" : "3",
            "d" : "4"
        }
    ],
    "correctAnswer" : "b",
    "marksAlloted" : "1"
}

Note: $replaceRoot came with version 3.4 of mongo.

James Risner
  • 5,451
  • 11
  • 25
  • 47
Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44