How to request a nested array of objects in mongodb?

I am trying to query a nested array of objects in mongodb from node js, tried all the solutions, but no luck. can anyone help with this on priority?

I tried the following:

{
              "name": "Science",
              "chapters": [
                 {
                     "name": "ScienceChap1",
                     "tests": [
                        {
                            "name": "ScienceChap1Test1",
                            "id": 1,
                            "marks": 10,
                            "duration": 30,
                            "questions": [
                               {
                                   "question": "What is the capital city of New Mexico?",
                                   "type": "mcq",
                                   "choice": [
                                      "Guadalajara",
                                      "Albuquerque",
                                      "Santa Fe",
                                      "Taos"
                                   ],
                                   "answer": [
                                      "Santa Fe",
                                      "Taos"
                                   ]
                               },
                               {
                                   "question": "Who is the author of beowulf?",
                                   "type": "notmcq",
                                   "choice": [
                                      "Mark Twain",
                                      "Shakespeare",
                                      "Abraham Lincoln",
                                      "Newton"
                                   ],
                                   "answer": [
                                      "Shakespeare"
                                   ]
                               }
                            ]
                        },
                        {
                            "name": "ScienceChap1test2",
                            "id": 2,
                            "marks": 20,
                            "duration": 30,
                            "questions": [
                               {
                                   "question": "What is the capital city of New Mexico?",
                                   "type": "mcq",
                                   "choice": [
                                      "Guadalajara",
                                      "Albuquerque",
                                      "Santa Fe",
                                      "Taos"
                                   ],
                                   "answer": [
                                      "Santa Fe",
                                      "Taos"
                                   ]
                               },
                               {
                                   "question": "Who is the author of beowulf?",
                                   "type": "notmcq",
                                   "choice": [
                                      "Mark Twain",
                                      "Shakespeare",
                                      "Abraham Lincoln",
                                      "Newton"
                                   ],
                                   "answer": [
                                      "Shakespeare"
                                   ]
                               }
                            ]
                        }
                     ]
                 }
              ]
          }

Here is what I've tried so far, but still can't get it to work

db.quiz.find({name:"Science"},{"tests":0,chapters:{$elemMatch:{name:"ScienceCh‌​ap1"}}})
db.quiz.find({ chapters: { $elemMatch: {$elemMatch: { name:"ScienceChap1Test1" } } }}) 
db.quiz.find({name:"Science"},{chapters:{$elemMatch:{$elemMatch:{name:"Scienc‌​eChap1Test1"}}}}) ({ name:"Science"},{ chapters: { $elemMatch: {$elemMatch: { name:"ScienceChap1Test1" } } }})
+4
source share
2 answers

Aggregate Structure

You can use the aggregation structure to convert and combine documents in collections for display to the client. You create a conveyor that processes the flow of documents through several building blocks: filtering, projecting, grouping, sorting, etc.

mcq "ScienceChap1Test1", :

db.quiz.aggregate(
    //Match the documents by query. Search for science course
    {"$match":{"name":"Science"}},

    //De-normalize the nested array of chapters.
    {"$unwind":"$chapters"},
    {"$unwind":"$chapters.tests"},

    //Match the document with test name Science Chapter
    {"$match":{"chapters.tests.name":"ScienceChap1test2"}},

    //Unwind nested questions array
    {"$unwind":"$chapters.tests.questions"},

    //Match questions of type mcq
    {"$match":{"chapters.tests.questions.type":"mcq"}}
).pretty()

:

{
    "_id" : ObjectId("5629eb252e95c020d4a0c5a5"),
    "name" : "Science",
    "chapters" : {
        "name" : "ScienceChap1",
        "tests" : {
            "name" : "ScienceChap1test2",
            "id" : 2,
            "marks" : 20,
            "duration" : 30,
            "questions" : {
                "question" : "What is the capital city of New Mexico?",
                "type" : "mcq",
                "choice" : [
                    "Guadalajara",
                    "Albuquerque",
                    "Santa Fe",
                    "Taos"
                ],
                "answer" : [
                    "Santa Fe",
                    "Taos"
                ]
            }
        }
    }
}

$elemMatch . $unwind.

, .

+2

javascript- mongodb.

.

1

, , , find.

, , . Science, :

db.quiz.find({name:"Science"})

$elemMatch. , ScienceChap1. :

db.quiz.find({"chapters":{"$elemMatch":{"name":"ScienceChap1"}}})

, , :

db.quiz.find({"chapters.tests":{"$elemMatch":{"name":"ScienceChap1Test1"}}})

- ,

, Return, , ( findOne), . , .

db.quiz.find({name:"Science"},{"chapters":1})

//Would return
{
    "_id": ObjectId(...),
    "chapters": [
        "name": "ScienceChap2",
        "tests: [..all object content here..]
}

, :

db.quiz.find({name:"Science"},{"chapters.tests.marks":1})

//Would return
{
    "_id": ObjectId(...),
    "chapters": [
        "tests: [
            {"marks":10},
            {"marks":20}
         ]
}

:

db.quiz.find({name:"Science"},{"chapters.tests.questions":1})

. , .

0

Source: https://habr.com/ru/post/1612779/


All Articles