您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

mongodb查询

2024/7/20 14:51:15发布15次查看
这节来说说如何检索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_comswitched 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查询, 感谢原作者分享。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product