Mongodb内嵌文档的查询

在《MongoDB权威指南》这本书中4.3.4 中有个查询文档的章节,里面用到了find和elemMatch,但是给出的例子不是很详细,所以理解起来有点困难,我们来用例子来演示下。

0.目的
为了在blog集合中找到由Joe发表的5分以上的评论

1.表结构如下

> db.blog.findOne()
{
        "_id" : ObjectId("55814703e6c873d825fdcc1a"),
        "content" : "...",
        "conments" : [
                {
                        "author" : "joe",
                        "score" : 3,
                        "comment" : "nice post"
                },
                {
                        "author" : "joe",
                        "score" : 6,
                        "comment" : "terrible post"
                }
        ]
}

2.构建测试样例

db.blog.insert({"content":"...", "conments":[ { "author":"joe", "score":3, "comment":"nice post" }, { "author":"joe", "score":6, "comment":"terrible post" } ] })

db.blog.insert({"content":"...", "conments":[ { "author":"joee", "score":3, "comment":"nice post" }, { "author":"joee", "score":6, "comment":"terrible post" } ] })

db.blog.insert({"content":"...", "conments":[ { "author":"joe", "score":3, "comment":"nice post" }, { "author":"joee", "score":6, "comment":"terrible post" } ] })

db.blog.insert({"content":"...", "conments":[ { "author":"joee", "score":3, "comment":"nice post" }, { "author":"joe", "score":6, "comment":"terrible post" } ] })
> db.blog.find()
{
        "_id" : ObjectId("55814703e6c873d825fdcc1a"),
        "content" : "...",
        "conments" : [
                {
                        "author" : "joe",
                        "score" : 3,
                        "comment" : "nice post"
                },
                {
                        "author" : "joe",
                        "score" : 6,
                        "comment" : "terrible post"
                }
        ]
}
{
        "_id" : ObjectId("55814711e6c873d825fdcc1b"),
        "content" : "...",
        "conments" : [
                {
                        "author" : "joee",
                        "score" : 3,
                        "comment" : "nice post"
                },
                {
                        "author" : "joee",
                        "score" : 6,
                        "comment" : "terrible post"
                }
        ]
}
{
        "_id" : ObjectId("55814719e6c873d825fdcc1c"),
        "content" : "...",
        "conments" : [
                {
                        "author" : "joe",
                        "score" : 3,
                        "comment" : "nice post"
                },
                {
                        "author" : "joee",
                        "score" : 6,
                        "comment" : "terrible post"
                }
        ]
}
{
        "_id" : ObjectId("55814725e6c873d825fdcc1d"),
        "content" : "...",
        "conments" : [
                {
                        "author" : "joee",
                        "score" : 3,
                        "comment" : "nice post"
                },
                {
                        "author" : "joe",
                        "score" : 6,
                        "comment" : "terrible post"
                }
        ]
}

3.测试1

db.blog.find({"comments" : {"author" : "joe",  "score": {"$gte" : 5 }}})

没有输出任何内容,所以不正确。

4.测试2

db.blog.find({“conments.author”:”joe”, “conments.score”:{“$gte”:5}})

结果如下:

db.blog.find({"conments.author":"joe", "conments.score":{"$gte":5}})
{
        "_id" : ObjectId("55814703e6c873d825fdcc1a"),
        "content" : "...",
        "conments" : [
                {
                        "author" : "joe",
                        "score" : 3,
                        "comment" : "nice post"
                },
                {
                        "author" : "joe",
                        "score" : 6,
                        "comment" : "terrible post"
                }
        ]
}
{
        "_id" : ObjectId("55814719e6c873d825fdcc1c"),
        "content" : "...",
        "conments" : [
                {
                        "author" : "joe",
                        "score" : 3,
                        "comment" : "nice post"
                },
                {
                        "author" : "joee",
                        "score" : 6,
                        "comment" : "terrible post"
                }
        ]
}
{
        "_id" : ObjectId("55814725e6c873d825fdcc1d"),
        "content" : "...",
        "conments" : [
                {
                        "author" : "joee",
                        "score" : 3,
                        "comment" : "nice post"
                },
                {
                        "author" : "joe",
                        "score" : 6,
                        "comment" : "terrible post"
                }
        ]
}

但是你会发现第二条文档并不满足条件也给find出来了。

5.正确做法

db.blog.find({"conments": { "$elemMatch" : { "author" : "joe", "score": {"$gte": 5} }}} )

结果如下:

{
        "_id" : ObjectId("55814703e6c873d825fdcc1a"),
        "content" : "...",
        "conments" : [
                {
                        "author" : "joe",
                        "score" : 3,
                        "comment" : "nice post"
                },
                {
                        "author" : "joe",
                        "score" : 6,
                        "comment" : "terrible post"
                }
        ]
}
{
        "_id" : ObjectId("55814725e6c873d825fdcc1d"),
        "content" : "...",
        "conments" : [
                {
                        "author" : "joee",
                        "score" : 3,
                        "comment" : "nice post"
                },
                {
                        "author" : "joe",
                        "score" : 6,
                        "comment" : "terrible post"
                }
        ]
}

你可能感兴趣的:(Mongodb)