登录  /  注册
mongodb查询
php中文网
发布: 2016-06-07 16:34:50
原创
781人浏览过

这节来说说如何检索mongodb数据。首先向文档中插入一些数据。1. 插入数据 use ttlsa_comswitched to db ttlsa_com db.mediaCollection.insert({ "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher"

这节来说说如何检索mongodb数据。首先向文档中插入一些数据。 1. 插入数据
> use ttlsa_com
switched to db ttlsa_com
> db.mediaCollection.insert({ "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher" : "Apress", "Author": [ "Membrey, Peter", "Plugge, Eelco", "Hawkins, Tim" ] })
> db.mediaCollection.insert({ "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind" })
> db.mediaCollection.insert({ "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells like teen spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ]})
> db.mediaCollection.find()
{ "_id" : ObjectId("5353462f93efef02c962da71"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher" : "Apress", "Author" : [ "Membrey, Peter", "Plugge, Eelco", "Hawkins, Tim" ] }
{ "_id" : ObjectId("5353462f93efef02c962da72"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind" }
{ "_id" : ObjectId("5353463193efef02c962da73"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells like teen spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }
登录后复制
2. 检索 find函数是经常用到的一个。前面的文章也有介绍到。下面看看有选择性的检索,查看你感兴趣的数据。 检索"Artist" : "Nirvana"的数据:
> db.mediaCollection.find({"Artist" : "Nirvana"}).toArray()
[
        {
                "_id" : ObjectId("5353462f93efef02c962da72"),
                "Type" : "CD",
                "Artist" : "Nirvana",
                "Title" : "Nevermind"
        },
        {
                "_id" : ObjectId("5353463193efef02c962da73"),
                "Type" : "CD",
                "Artist" : "Nirvana",
                "Title" : "Nevermind",
                "Tracklist" : [
                        {
                                "Track" : "1",
                                "Title" : "Smells like teen spirit",
                                "Length" : "5:02"
                        },
                        {
                                "Track" : "2",
                                "Title" : "In Bloom",
                                "Length" : "4:15"
                        }
                ]
        }
]
登录后复制
上面的查询虽说检索出"Artist" : "Nirvana"的数据,但是返回了全部列的信息,但是我只要查看Title和Tracklist.Title列
> db.mediaCollection.find({"Artist" : "Nirvana"}, {Title:1, "Tracklist.Title":1}).toArray()
[
        {
                "_id" : ObjectId("5353462f93efef02c962da72"),
                "Title" : "Nevermind"
        },
        {
                "_id" : ObjectId("5353463193efef02c962da73"),
                "Title" : "Nevermind",
                "Tracklist" : [
                        {
                                "Title" : "Smells like teen spirit"
                        },
                        {
                                "Title" : "In Bloom"
                        }
                ]
        }
]
登录后复制
Title:1, "Tracklist.Title":1表示只返回这两列信息。升序。也可以反着来Title:0, "Tracklist.Title":0表示返回除了这两列的其他所有列信息。 注意:_id字段总是会返回。 3. ?使用逗号 当文档结构变的复杂时,如含有数组或嵌入对象文档,就需要使用到逗号,来检索嵌入在文档中的信息。
> db.mediaCollection.find({"Tracklist.Length":"5:02"}).toArray()
[
        {
                "_id" : ObjectId("5353463193efef02c962da73"),
                "Type" : "CD",
                "Artist" : "Nirvana",
                "Title" : "Nevermind",
                "Tracklist" : [
                        {
                                "Track" : "1",
                                "Title" : "Smells like teen spirit",
                                "Length" : "5:02"
                        },
                        {
                                "Track" : "2",
                                "Title" : "In Bloom",
                                "Length" : "4:15"
                        }
                ]
        }
]
登录后复制
查询整个内嵌文档:
> db.mediaCollection.find({Tracklist:{"Length":"5:02"}}).toArray()
[ ]
> db.mediaCollection.find({Tracklist:{"Track" : "1","Title" : "Smells like teen spirit","Length":"5:02"}}).toArray()
[
        {
                "_id" : ObjectId("5353463193efef02c962da73"),
                "Type" : "CD",
                "Artist" : "Nirvana",
                "Title" : "Nevermind",
                "Tracklist" : [
                        {
                                "Track" : "1",
                                "Title" : "Smells like teen spirit",
                                "Length" : "5:02"
                        },
                        {
                                "Track" : "2",
                                "Title" : "In Bloom",
                                "Length" : "4:15"
                        }
                ]
        }
]
> db.mediaCollection.find({Tracklist:{"Track" : "1","Length" : "5:02","Title" : "Smells like teen spirit"}}).toArray()
[ ]
登录后复制
查询整个文档需要全部列出内嵌文档的字段,且顺序要一致,否则匹配不到。 查询内嵌文档的多个字段。如查询有joe发表且分数在5分以上:
> db.mediaCollection.insert({ "content" : "...", "comments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, { "author" : "mary", "score" : 6, "comment" : "terrible post" } ] })
> db.mediaCollection.find().toArray()
[
        {
                "_id" : ObjectId("5353462f93efef02c962da71"),
                "Type" : "Book",
                "Title" : "Definitive Guide to MongoDB, the",
                "ISBN" : "987-1-4302-3051-9",
                "Publisher" : "Apress",
                "Author" : [
                        "Membrey, Peter",
                        "Plugge, Eelco",
                        "Hawkins, Tim"
                ]
        },
        {
                "_id" : ObjectId("5353462f93efef02c962da72"),
                "Type" : "CD",
                "Artist" : "Nirvana",
                "Title" : "Nevermind"
        },
        {
                "_id" : ObjectId("5353463193efef02c962da73"),
                "Type" : "CD",
                "Artist" : "Nirvana",
                "Title" : "Nevermind",
                "Tracklist" : [
                        {
                                "Track" : "1",
                                "Title" : "Smells like teen spirit",
                                "Length" : "5:02"
                        },
                        {
                                "Track" : "2",
                                "Title" : "In Bloom",
                                "Length" : "4:15"
                        }
                ]
        },
        {
                "_id" : ObjectId("5353681293efef02c962da7a"),
                "content" : "...",
                "comments" : [
                        {
                                "author" : "joe",
                                "score" : 3,
                                "comment" : "nice post"
                        },
                        {
                                "author" : "mary",
                                "score" : 6,
                                "comment" : "terrible post"
                        }
                ]
        }
]
> db.mediaCollection.find({"comments" : {"author" : "joe", "score" : {"$gte" : 5}}}).toArray()
[ ]
> db.mediaCollection.find({"comments.author" : "joe", "comments.score" : {"$gte" : 5}}).toArray()
[
        {
                "_id" : ObjectId("5353681293efef02c962da7a"),
                "content" : "...",
                "comments" : [
                        {
                                "author" : "joe",
                                "score" : 3,
                                "comment" : "nice post"
                        },
                        {
                                "author" : "mary",
                                "score" : 6,
                                "comment" : "terrible post"
                        }
                ]
        }
]
登录后复制
上面的查询是不对的。 要正确的指定一组条件,而不是每个键,因此要使用到$elemMatch。这样就可以用来部分指定匹配数组中的单个内嵌文档的限定条件。正确的写法如下所示:
> db.mediaCollection.find({"comments" : {"$elemMatch" : {"author" : "joe", "score" : {"$gte" : 5}}}}).toArray()
[ ]
登录后复制
对于数组:
> db.mediaCollection.find({"Author":"Membrey, Peter"}).toArray()
[
        {
                "_id" : ObjectId("5353462f93efef02c962da71"),
                "Type" : "Book",
                "Title" : "Definitive Guide to MongoDB, the",
                "ISBN" : "987-1-4302-3051-9",
                "Publisher" : "Apress",
                "Author" : [
                        "Membrey, Peter",
                        "Plugge, Eelco",
                        "Hawkins, Tim"
                ]
        }
]
登录后复制
正则表达式查询:
> db.mediaCollection.find({"Title":/MongoDB/i}).toArray()
[
        {
                "_id" : ObjectId("5353462f93efef02c962da71"),
                "Type" : "Book",
                "Title" : "Definitive Guide to MongoDB, the",
                "ISBN" : "987-1-4302-3051-9",
                "Publisher" : "Apress",
                "Author" : [
                        "Membrey, Peter",
                        "Plugge, Eelco",
                        "Hawkins, Tim"
                ]
        }
]
登录后复制
对检索结果进行Sort, Limit, 和Skip请看下节内容。

来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学