MongoDB学习笔记六—查询下

发布时间:2017-2-25 12:36:06 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"MongoDB学习笔记六—查询下 ",主要涉及到MongoDB学习笔记六—查询下 方面的内容,对于MongoDB学习笔记六—查询下 感兴趣的同学可以参考一下。

查询内嵌文档

数据准备

> db.blog.find().pretty()
{
        "_id" : ObjectId("585694e4c5b0525a48a441b5"),
        "content" : "...",
        "comments" : [
                {
                        "comment" : "good post",
                        "author" : "jim",
                        "votes" : 15
                },
                {
                        "comment" : "i thought it was too short",
                        "author" : "yyb",
                        "votes" : 3
                },
                {
                        "comment" : "free watches",
                        "author" : "alice",
                        "votes" : 3
                }
        ]
}
{
        "_id" : ObjectId("585f659898992d5a44085fc0"),
        "content" : "query",
        "comments" : [
                {
                        "comment" : "good post two",
                        "author" : "yyb",
                        "votes" : 10
                },
                {
                        "comment" : "i thought it was vary good",
                        "author" : "mmq",
                        "votes" : 30
                },
                {
                        "comment" : "free watches two",
                        "author" : "lf",
                        "votes" : 20
                }
        ]
}
> 

方式1:查询整个内嵌文档

与普通查询完全相同。但是,如果要查询一个完整的子文档,那么子文档必须精确匹配(顺序以及个数都要一样)。

 > db.blog.find({"comments":{"author":"lf","votes":20}})
 >

> db.blog.find({"comments":{"comment":"free watches two","author":"lf","votes":20}}).pretty()
{
        "_id" : ObjectId("585f659898992d5a44085fc0"),
        "content" : "query",
        "comments" : [
                {
                        "comment" : "good post two",
                        "author" : "yyb",
                        "votes" : 10
                },
                {
                        "comment" : "i thought it was vary good",
                        "author" : "mmq",
                        "votes" : 30
                },
                {
                        "comment" : "free watches two",
                        "author" : "lf",
                        "votes" : 20
                }
        ]
}
> 

方式2:只针对其键/值对进行查询

> db.blog.find({"comments.author":"yyb","comments.votes":10}).pretty()
{
        "_id" : ObjectId("585f659898992d5a44085fc0"),
        "content" : "query",
        "comments" : [
                {
                        "comment" : "good post two",
                        "author" : "yyb",
                        "votes" : 10
                },
                {
                        "comment" : "i thought it was vary good",
                        "author" : "mmq",
                        "votes" : 30
                },
                {
                        "comment" : "free watches two",
                        "author" : "lf",
                        "votes" : 20
                }
        ]
}

如果想要查找由yyb发表的11分以上的投票。不能直接使用以下方式

> db.blog.find({"comments":{"author":"yyb","votes":{"$gt":11}}})
> 

 或者

> db.blog.find({"comments.author":"yyb","comments.votes":{"$gt":11}})//本来没有,但却查出来两条
{ "_id" : ObjectId("585694e4c5b0525a48a441b5"), "content" : "...", "comments" : [ { "comment" : "good post", "author" : "jim", "votes" : 15 }, { "comment" : "i thought it was too short", "author" : "yyb", "votes" : 3 }, { "comment" : "free watches", "author" : "alice", "votes" : 3 } ] }
{ "_id" : ObjectId("585f659898992d5a44085fc0"), "content" : "query", "comments" : [ { "comment" : "good post two", "author" : "yyb", "votes" : 10 }, { "comment" : "i thought it was vary good", "author" : "mmq", "votes" : 30 }, { "comment" : "free watches two", "author" : "lf", "votes" : 20 } ] }

$elemMatch

要正确地指定一组条件,而不必指定每个键,就需要使用 $elemMatch 。这种模糊的命名条件句能用来在查询条件中部分指定匹配数组中的单个内嵌文档。

$elemMatch将限定条件进行分组,仅当需要对一个内嵌文档的多个键操作时才会用到。

> db.blog.find({"comments":{"$elemMatch":{"author":"yyb","votes":{"$gt":11}}}})//无匹配结果
> db.blog.find({"comments":{"$elemMatch":{"author":"yyb","votes":{"$gte":10}}}}).pretty() { "_id" : ObjectId("585f659898992d5a44085fc0"), "content" : "query", "comments" : [ { "comment" : "good post two", "author" : "yyb", "votes" : 10 }, { "comment" : "i thought it was vary good", "author" : "mmq", "votes" : 30 }, { "comment" : "free watches two", "author" : "lf", "votes" : 20

上一篇:golang: 把sql结果集以json格式输出
下一篇:微软发布VSBT,无需安装Visual Studio即可实现项目编译

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。