这节来说说如何检索mongodb数据。首先向文档中插入一些数据。
1. 插入数据文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
> 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. 检索文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
find函数是经常用到的一个。前面的文章也有介绍到。下面看看有选择性的检索,查看你感兴趣的数据。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
检索"Artist" : "Nirvana"的数据:文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
> 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列文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
> 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表示返回除了这两列的其他所有列信息。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
注意:_id字段总是会返回。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
3. 使用逗号文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
当文档结构变的复杂时,如含有数组或嵌入对象文档,就需要使用到逗号,来检索嵌入在文档中的信息。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
> 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" } ] } ]
查询整个内嵌文档:文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
> 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() [ ]
查询整个文档需要全部列出内嵌文档的字段,且顺序要一致,否则匹配不到。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
查询内嵌文档的多个字段。如查询有joe发表且分数在5分以上:文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
> 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" } ] } ]
上面的查询是不对的。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
要正确的指定一组条件,而不是每个键,因此要使用到$elemMatch。这样就可以用来部分指定匹配数组中的单个内嵌文档的限定条件。正确的写法如下所示:文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
> db.mediaCollection.find({"comments" : {"$elemMatch" : {"author" : "joe", "score" : {"$gte" : 5}}}}).toArray() [ ]
对于数组:文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
> 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" ] } ]
正则表达式查询:文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
> 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请看下节内容。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/ 文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-query-data-operator/
评论