MongoDB:深层嵌入式文档的查询替代方案?

时间:2014-02-27 16:18:27

标签: mongodb

我的mongoDB中有这个文档:

/* 0 */
{
    "bookTitle" : "HTML",
    "bookOrder" : 500,
    "_id" : ObjectId("530dea1d2dbf280000533b60"),
    "bookChapters" : [ 
        {
            "chapterTitle" : "chapterTitle",
            "chapterIntro" : "ChapterIntro",
            "chapterOrder" : 500,
            "_id" : ObjectId("530dea1d2dbf280000533b61"),
            "chapterArticles" : [ 
                {
                    "articleTitle" : "articleTitle",
                    "articleIntro" : "articleIntro",
                    "articleOrder" : 500,
                    "_id" : ObjectId("530dea1d2dbf280000533b62"),
                    "articleHeadings" : [ 
                        {
                            "headingTitle" : "headingTitle",
                            "headingOrder" : 500,
                            "_id" : ObjectId("530dea1d2dbf280000533b63")
                        }
                    ]
                }
            ]
        }
    ],
    "__v" : 0
}

要.find()深度嵌入字段“headingTitle”的值,我必须使用此查询:

/*
 db.bookcollections.findOne({"bookChapters.chapterArticles.articleHeadings.headingTitle":"headingTitle"}, {"bookChapters.chapterArticles.articleHeadings.headingTitle":1}).bookChapters[0].chapterArticles[0].articleHeadings[0].headingTitle

*/

关于最佳做法:

  1. 这不是很啰嗦吗?
  2. 这是正确的用法吗?
  3. 我是否需要重新考虑文档的结构?

0 个答案:

没有答案
相关问题