MongoDB中常用的find

接着前一篇文章,下面主要介绍一下MongoDB中常用的find操作。

先打开MongoDB shell,通过下面一组命令插入一些数据。

 post1 = {"title":"learn MongoDB", "author":"Wilber", "date":new Date(), "score":}
post2 = {"title":"learn English", "author":"Will", "date":new Date(), "score":}
post3 = {"title":"learn C#", "author":"Li", "date":new Date(), "score":}
post4 = {"title":"learn SQL", "author":"July", "date":new Date(), "score":}
post5 = {"title":"learn Node", "author":"Wilber", "date":new Date(), "score":}
db.blog.posts.insert(post1)
db.blog.posts.insert(post2)
db.blog.posts.insert(post3)
db.blog.posts.insert(post4)
db.blog.posts.insert(post5) users1 = ["Wilber", "Will", "June"]
users2 = ["Will", "July", "Wilber"]
users3 = ["James", "Jack", "Will"]
db.blog.users.insert({"users":users1})
db.blog.users.insert({"users":users2})
db.blog.users.insert({"users":users3})

find(arg1,arg2)简介

find查询会返回一个Collection中文档的子集。关于find的两个参数,arg1表示匹配条件,arg2可以指定要返回的键。

直接上例子,对于arg2,可以通过设置值为0,来控制那些键不要显示

 > db.blog.posts.find({"author":"Will"})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
> db.blog.posts.find({}, {"title":, "score":})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "score" : }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "score" : }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "score" : }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "score" : }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "score" : }
> db.blog.posts.find({}, {"date": })
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "score" : }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "score" : }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "score" : }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "score" : }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "score" : }

常用查询条件

比较条件:$gt,$gte,$lt,$lte,$ne

 > db.blog.posts.find({"score":{"$gte":,"$lt":}})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93 }
> db.blog.posts.find({"author":{"$ne":"July"}})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
:.979Z"), "score" : 80 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93 }
>

包含条件:$in,$nin

 > db.blog.posts.find({"author":{"$in":["Wilber","Will"]}})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93 }
> db.blog.posts.find({"author":{"$nin":["Wilber","Will"]}})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
:.979Z"), "score" : 80 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T1
::.989Z"), "score" : 70 }
>

或操作:$or

 > db.blog.posts.find({"$or":[{"author":"Wilber"}, {"score":{"$gt":}}]})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93 }
>

limit(),skip(),sort()

 > db.blog.posts.find().limit()
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
> db.blog.posts.find().skip()
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
:.979Z"), "score" : 80 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T1
::.989Z"), "score" : 70 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93 }
> db.blog.posts.find().sort({"athor":,"score":-})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
:.979Z"), "score" : 80 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T1
::.989Z"), "score" : 70 }
>

数组的查询

  1. $all:通过多个元素匹配数组
  2. 支持“键.下标”方式的匹配
  3. 支持$size的方式匹配
 > db.blog.users.find({"users":"Will"})
{ "_id" : ObjectId("5479d03d421b7f1536cfb20c"), "users" : [ "Wilber", "Will", "June" ] }
{ "_id" : ObjectId("5479d03d421b7f1536cfb20d"), "users" : [ "Will", "July", "Wilber" ] }
{ "_id" : ObjectId("5479d03d421b7f1536cfb20e"), "users" : [ "James", "Jack", "Will" ] }
> db.blog.users.find({"users": {"$all": ["Wilber", "Will"]}})
{ "_id" : ObjectId("5479d03d421b7f1536cfb20c"), "users" : [ "Wilber", "Will", "June" ] }
{ "_id" : ObjectId("5479d03d421b7f1536cfb20d"), "users" : [ "Will", "July", "Wilber" ] }
> db.blog.users.find({"users": ["Wilber", "Will"]})
> db.blog.users.find({"users.2":"Will"})
{ "_id" : ObjectId("5479d03d421b7f1536cfb20e"), "users" : [ "James", "Jack", "Will" ] }
> db.blog.users.find({"users":{"$size":}})
{ "_id" : ObjectId("5479d03d421b7f1536cfb20c"), "users" : [ "Wilber", "Will", "June" ] }
{ "_id" : ObjectId("5479d03d421b7f1536cfb20d"), "users" : [ "Will", "July", "Wilber" ] }
{ "_id" : ObjectId("5479d03d421b7f1536cfb20e"), "users" : [ "James", "Jack", "Will" ] }
>

null的含义

 > post5.z = null
null
> db.blog.posts.update({"title":"learn Node"}, post5)
> db.blog.posts.find({"z":null})
{ "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T13::.939Z"), "score" : 90 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T13::.959Z"), "score" : 95 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T13:2
:.979Z"), "score" : 80 }
{ "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T1
::.989Z"), "score" : 70 }
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93, "z" : null }
> db.blog.posts.find({"z":{"$in":[null], "$exists": true}})
{ "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "date" : ISODate("2014-11-2
9T13::.999Z"), "score" : 93, "z" : null }
>

Ps:可以通过以下链接的到例子中的shell命令。

http://files.cnblogs.com/wilber2013/find.js

上一篇:NTP配置实践


下一篇:一些通过SAP ABAP代码审查得出的ABAP编程最佳实践