mongodb查询
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mongodb查询,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6224字,纯文字阅读大概需要9分钟。
内容图文
这节来说说如何检索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请看下节内容。
原文地址:mongodb查询, 感谢原作者分享。
内容总结
以上是互联网集市为您收集整理的mongodb查询全部内容,希望文章能够帮你解决mongodb查询所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。