2017-11-04 1 views
1

Mein Dokument enthält fast sieben Ebenen des Dokuments in MongoDB, jetzt muss ich eine Abfrage schreiben, um alle Bedingungen wie überprüfen, subject=java, topic=oops, level=l1, complexity=easy, questionType=mcq basierend auf dieser Anforderung alle Fragen in der Sammlung anzuzeigen. Bitte helfen Sie mir, Danke im Voraus.Abfrage für eingebettetes Dokument in MongoDb?

{ 
    "_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" 
               } 
              ] 
             } 
            ] 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 
+0

Welche Ausgabe/query erwarten Sie? – Astro

+0

Abfrage nach was benötigen Sie? Sie können einfach 'db.collection.find ({" subjectLists.subject ":" java "}, {" subjectLists.topicList.levelList.complexityList.questionTypeList.questionList ": 1})' aufrufen, das alle Fragen zurückgibt, die zu 'passen subject = java' –

+0

subject = java, topic = oops, level = l1, complexity = easy, fragmentType = mcq basierend auf dieser Anforderung zeigen alle Fragen in der Sammlung an. –

Antwort

0

Sie dieses Skript ausprobieren kann.

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" } 
} 
]) 

Ergebnis:

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

subject = java, topic = oops, level = l1, complexity = easy, fragmentType = mcq basierend auf dieser Anforderung werden alle Fragen in der Sammlung angezeigt. –

+0

Ich habe ein Update gemacht. –

+0

Ich versuche dieses Skript auszuführen, aber ich erhalte diesen Fehler, "errmsg": "Ausnahme: Unbekannter Name der Pipelinestufe: '$ replaceRoot'", –

0

folgende Abfrage mit Projektionslisten nur Fragen:

Hinweis: Geben Sie Ihre Anfrage unter:

db.questions.findOne({}, {'subjectLists.topicList.levelList.complexityList.questionTypeList.questionList':1}) 
Verwandte Themen