如何在mongodb中查询嵌套的对象数组?

时间:2015-10-22 15:14:00

标签: node.js mongodb-query

我试图从节点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" } } }})

2 个答案:

答案 0 :(得分:3)

汇总框架

您可以使用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将聚合框架用于“数组过滤”。

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

答案 1 :(得分:0)

您应该在mongodb简单的javascript shell中尝试以下查询。

可能有两种情景。

情景一

如果您只想返回包含某些章节名称或测试名称的文档,例如,find中只有一个参数可以。

对于find方法,您要返回的文档由第一个参数指定。您可以通过执行以下操作返回名为Science的文档:

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

您可以使用$elemMatch指定匹配数组中单个嵌入文档的条件。查找具有名称ScienceChap1的章节的文档。你可以这样做:

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

如果您希望您的条件成为测试名称,那么您可以使用点运算符,如下所示:

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

场景二 - 指定要返回的键

如果要指定返回哪些键,可以传递第二个参数来查找(或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})

测试这些。我希望这些帮助。