当前位置:Gxlcms > mysql > mongodb查询

mongodb查询

时间:2021-07-01 10:21:17 帮助过:14人阅读

这节来说说如何检索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请看下节内容。

人气教程排行