node.js – 如何在mongodb中查询嵌套的对象数组?

我试图从节点js查询
mongodb中嵌套的对象数组,尝试了所有的解决方案,但没有运气.有谁可以请优先帮助这个?

我试过以下:

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

这是我到目前为止所尝试的但仍无法使其工作

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

最佳答案 聚合框架

您可以使用aggregation framework转换和合并集合中的文档以显示给客户端.您构建了一个管道,通过几个构建块处理文档流:过滤,投影,分组,排序等.

如果您想从名为“ScienceChap1Test1”的测试中获取mcq类型的问题,您将执行以下操作:

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将聚合框架用于“阵列过滤”.

您可以从上面代码中的聚合管道中的每个命令的底部删除每一行,以观察管道行为.

点赞