设为首页 收藏本站
查看: 578|回复: 0

[经验分享] MongoDB中常用的find

[复制链接]

尚未签到

发表于 2015-7-6 11:35:39 | 显示全部楼层 |阅读模式
  接着前一篇文章,下面主要介绍一下MongoDB中常用的find操作。
  先打开MongoDB shell,通过下面一组命令插入一些数据。



1 post1 = {"title":"learn MongoDB", "author":"Wilber", "date":new Date(), "score":90}
2 post2 = {"title":"learn English", "author":"Will", "date":new Date(), "score":95}
3 post3 = {"title":"learn C#", "author":"Li", "date":new Date(), "score":80}
4 post4 = {"title":"learn SQL", "author":"July", "date":new Date(), "score":70}
5 post5 = {"title":"learn Node", "author":"Wilber", "date":new Date(), "score":93}
6 db.blog.posts.insert(post1)
7 db.blog.posts.insert(post2)
8 db.blog.posts.insert(post3)
9 db.blog.posts.insert(post4)
10 db.blog.posts.insert(post5)
11
12 users1 = ["Wilber", "Will", "June"]
13 users2 = ["Will", "July", "Wilber"]
14 users3 = ["James", "Jack", "Will"]
15 db.blog.users.insert({"users":users1})
16 db.blog.users.insert({"users":users2})
17 db.blog.users.insert({"users":users3})
  

find(arg1,arg2)简介
  find查询会返回一个Collection中文档的子集。关于find的两个参数,arg1表示匹配条件,arg2可以指定要返回的键。
  直接上例子,对于arg2,可以通过设置值为0,来控制那些键不要显示



1 > db.blog.posts.find({"author":"Will"})
2 { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
3 29T13:28:17.959Z"), "score" : 95 }
4 > db.blog.posts.find({}, {"title":1, "score":1})
5 { "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "score" : 90 }
6 { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "score" : 95 }
7 { "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "score" : 80 }
8 { "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "score" : 70 }
9 { "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "score" : 93 }
10 > db.blog.posts.find({}, {"date": 0})
11 { "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "score" : 90 }
12 { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "score" : 95 }
13 { "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "score" : 80 }
14 { "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "score" : 70 }
15 { "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "score" : 93 }
  

常用查询条件
  比较条件:$gt,$gte,$lt,$lte,$ne



1 > db.blog.posts.find({"score":{"$gte":90,"$lt":95}})
2 { "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
3 1-29T13:28:17.939Z"), "score" : 90 }
4 { "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
5 9T13:28:17.999Z"), "score" : 93 }
6 > db.blog.posts.find({"author":{"$ne":"July"}})
7 { "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
8 1-29T13:28:17.939Z"), "score" : 90 }
9 { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
10 29T13:28:17.959Z"), "score" : 95 }
11 { "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
12 8:17.979Z"), "score" : 80 }
13 { "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
14 9T13:28:17.999Z"), "score" : 93 }
15 >
  
  包含条件:$in,$nin



1 > db.blog.posts.find({"author":{"$in":["Wilber","Will"]}})
2 { "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
3 1-29T13:28:17.939Z"), "score" : 90 }
4 { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
5 29T13:28:17.959Z"), "score" : 95 }
6 { "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
7 9T13:28:17.999Z"), "score" : 93 }
8 > db.blog.posts.find({"author":{"$nin":["Wilber","Will"]}})
9 { "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
10 8:17.979Z"), "score" : 80 }
11 { "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T1
12 3:28:17.989Z"), "score" : 70 }
13 >
  
  或操作:$or



1 > db.blog.posts.find({"$or":[{"author":"Wilber"}, {"score":{"$gt":90}}]})
2 { "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
3 1-29T13:28:17.939Z"), "score" : 90 }
4 { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
5 29T13:28:17.959Z"), "score" : 95 }
6 { "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
7 9T13:28:17.999Z"), "score" : 93 }
8 >
  
  limit(),skip(),sort()



1 > db.blog.posts.find().limit(2)
2 { "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
3 1-29T13:28:17.939Z"), "score" : 90 }
4 { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
5 29T13:28:17.959Z"), "score" : 95 }
6 > db.blog.posts.find().skip(1)
7 { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
8 29T13:28:17.959Z"), "score" : 95 }
9 { "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
10 8:17.979Z"), "score" : 80 }
11 { "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T1
12 3:28:17.989Z"), "score" : 70 }
13 { "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
14 9T13:28:17.999Z"), "score" : 93 }
15 > db.blog.posts.find().sort({"athor":1,"score":-1})
16 { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
17 29T13:28:17.959Z"), "score" : 95 }
18 { "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
19 9T13:28:17.999Z"), "score" : 93 }
20 { "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
21 1-29T13:28:17.939Z"), "score" : 90 }
22 { "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
23 8:17.979Z"), "score" : 80 }
24 { "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T1
25 3:28:17.989Z"), "score" : 70 }
26 >
  
  数组的查询


  • $all:通过多个元素匹配数组
  • 支持“键.下标”方式的匹配
  • 支持$size的方式匹配



1 > db.blog.users.find({"users":"Will"})
2 { "_id" : ObjectId("5479d03d421b7f1536cfb20c"), "users" : [  "Wilber",  "Will",  "June" ] }
3 { "_id" : ObjectId("5479d03d421b7f1536cfb20d"), "users" : [  "Will",  "July",  "Wilber" ] }
4 { "_id" : ObjectId("5479d03d421b7f1536cfb20e"), "users" : [  "James",  "Jack",  "Will" ] }
5 > db.blog.users.find({"users": {"$all": ["Wilber", "Will"]}})
6 { "_id" : ObjectId("5479d03d421b7f1536cfb20c"), "users" : [  "Wilber",  "Will",  "June" ] }
7 { "_id" : ObjectId("5479d03d421b7f1536cfb20d"), "users" : [  "Will",  "July",  "Wilber" ] }
8 > db.blog.users.find({"users": ["Wilber", "Will"]})
9 > db.blog.users.find({"users.2":"Will"})
10 { "_id" : ObjectId("5479d03d421b7f1536cfb20e"), "users" : [  "James",  "Jack",  "Will" ] }
11 > db.blog.users.find({"users":{"$size":3}})
12 { "_id" : ObjectId("5479d03d421b7f1536cfb20c"), "users" : [  "Wilber",  "Will",  "June" ] }
13 { "_id" : ObjectId("5479d03d421b7f1536cfb20d"), "users" : [  "Will",  "July",  "Wilber" ] }
14 { "_id" : ObjectId("5479d03d421b7f1536cfb20e"), "users" : [  "James",  "Jack",  "Will" ] }
15 >
  
  null的含义



1 > post5.z = null
2 null
3 > db.blog.posts.update({"title":"learn Node"}, post5)
4 > db.blog.posts.find({"z":null})
5 { "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
6 1-29T13:28:17.939Z"), "score" : 90 }
7 { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
8 29T13:28:17.959Z"), "score" : 95 }
9 { "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
10 8:17.979Z"), "score" : 80 }
11 { "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T1
12 3:28:17.989Z"), "score" : 70 }
13 { "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
14 9T13:28:17.999Z"), "score" : 93, "z" : null }
15 > db.blog.posts.find({"z":{"$in":[null], "$exists": true}})
16 { "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
17 9T13:28:17.999Z"), "score" : 93, "z" : null }
18 >
  
  Ps:可以通过以下链接的到例子中的shell命令。
  http://files.iyunv.com/wilber2013/find.js

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-83795-1-1.html 上篇帖子: MongoDB的自定义序列化(Customizing serialization) 下篇帖子: 数据库:mongodb与关系型数据库相比的优缺点zz
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表