查询条件
首先往数据库集合里面插入几条数据。
测试数据:
> db.users.insert({username:"mongo", url:"webinglin.github.io", tags:["mongodb", database","nosql"],likes:999, author:"linwenbin"})
> db.users.insert({username:"redis", url:"webinglin.github.io", tags:["redis","database","nosql"],likes:888, author:"linwenbin"})
> db.users.insert({username:"spring", url:"webinglin.github.io", tags:["spring","framework"],likes:777, author:"linwenbin"})
> db.users.find().pretty()
{
"_id" : ObjectId("5574bdabc705777157a515aa"),
"username" : "mongo",
"url" : "webinglin.github.io",
"tags" : [
"mongodb",
"database",
"nosql"
],
"likes" : 999,
"author" : "linwenbin"
}
{
"_id" : ObjectId("5574bdd2c705777157a515ab"),
"username" : "redis",
"url" : "webinglin.github.io",
"tags" : [
"redis",
"database",
"nosql"
],
"likes" : 888,
"author" : "linwenbin"
}
{
"_id" : ObjectId("5574bdf3c705777157a515ac"),
"username" : "spring",
"url" : "webinglin.github.io",
"tags" : [
"spring",
"framework"
],
"likes" : 777,
"author" : "linwenbin"
}
pretty() 方法是对查询结果进行格式化
查询的时候可以带上查询条件,那具体的查询条件怎么使用?
等于
等于操作直接使用 {key:value} 这样的文档形式即可
> db.users.find({username:"mongo"})
{ "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" }
>
大于
语法: {key : {$gt:value} }
> db.users.find({likes:{$gt:888}})
{ "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" }
>
大于等于
语法: {key : {$gte:value} }
> db.users.find({likes:{$gte:888}})
{ "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" }
{ "_id" : ObjectId("5574bdd2c705777157a515ab"), "username" : "redis", "url" : "webinglin.github.io", "tags" : [ "redis", "database", "nosql" ], "likes" : 888, "author" : "linwenbin" }
小于
语法: {key : {$lt:value} }
> db.users.find({likes:{$lt:888}})
{ "_id" : ObjectId("5574bdf3c705777157a515ac"), "username" : "spring", "url" : "webinglin.github.io", "tags" : [ "spring", "framework" ], "likes" : 777, "author" : "linwenbin" }
小于等于
语法: {key : {$lte:value}}
> db.users.find({likes:{$lte:888}})
{ "_id" : ObjectId("5574bdd2c705777157a515ab"), "username" : "redis", "url" : "webinglin.github.io", "tags" : [ "redis", "database", "nosql" ], "likes" : 888, "author" : "linwenbin" }
{ "_id" : ObjectId("5574bdf3c705777157a515ac"), "username" : "spring", "url" : "webinglin.github.io", "tags" : [ "spring", "framework" ], "likes" : 777, "author" : "linwenbin" }
不等于
语法: {key : {$ne:value} }
> db.users.find({likes:{$ne:888}})
{ "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" }
{ "_id" : ObjectId("5574bdf3c705777157a515ac"), "username" : "spring", "url" : "webinglin.github.io", "tags" : [ "spring", "framework" ], "likes" : 777, "author" : "linwenbin" }
且操作 AND
语法: {key1:value1, key2:value2, key3:value3 …}
> db.users.find({likes:{$gt:777},username:"mongo"})
{ "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" }
> db.users.find({likes:{$gt:777}})
{ "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" }
{ "_id" : ObjectId("5574bdd2c705777157a515ab"), "username" : "redis", "url" : "webinglin.github.io", "tags" : [ "redis", "database", "nosql" ], "likes" : 888, "author" : "linwenbin" }
或操作 OR
语法: { $or: [ {key1: value1}, {key2:value2} ] } 将or条件的所有 {key:value} 都放在 $or 的value中(数组)
> db.users.find({$or:[{username:"mongo"},{username:"redis"}]})
{ "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" }
{ "_id" : ObjectId("5574bdd2c705777157a515ab"), "username" : "redis", "url" : "webinglin.github.io", "tags" : [ "redis", "database", "nosql" ], "likes" : 888, "author" : "linwenbin" }
复杂条件查询
如何将所有的条件都连起来用呢?
比如我们想要这样查询 like>=888 && (username=”mongo” or username=”spring”)
由于上面的数据只有三条, 我们知道 like>=888 只有 mongo 和 redis 这两条数据满足条件, 后面的username=”mongo” or username=”spring” 又有 mongo和 spring 满足条件, 这两个and操作之后 就只剩下 mongo 这条数据满足条件了。 所以最终应该查出一条mongo的Document.
> db.users.find({likes:{$gte:888},$or:[{username:"mongo"},{username:"spring"}]})
{ "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" }
>
find() 其他用法
Projection
mongodb中 projection 意味着显示你希望看到的字段而非所有的字段都显示,这是什么意思呢?
比如: 我们的测试数据里面有那么多的字段: username,likes,tags,author,url 而我们经常要用到的就只有 username 和 likes 那么就显示这两个字段就好了,其他的字段就别显示出来了。
find({},{KEY:1/0}) find的第二个参数,KEY为要显示或隐藏的字段,value为1表示显示,0表示隐藏,看着也很简单,试一下吧
> db.users.find({},{_id:0,url:0,tags:0,author:0})
{ "username" : "mongo", "likes" : 999 }
{ "username" : "redis", "likes" : 888 }
{ "username" : "spring", "likes" : 777 }
>
limit, skip, sort
为了更好的测试分页的效果,新建一个集合,并插入30条数据
> for(var i=0; i<30; i++){
... db.pages.insert({"val":i});
... }
WriteResult({ "nInserted" : 1 })
> db.pages.find()
{ "_id" : ObjectId("5574ca7b192e9dda0925e37f"), "val" : 0 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e380"), "val" : 1 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e381"), "val" : 2 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e382"), "val" : 3 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e383"), "val" : 4 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e384"), "val" : 5 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e385"), "val" : 6 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e386"), "val" : 7 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e387"), "val" : 8 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e388"), "val" : 9 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e389"), "val" : 10 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38a"), "val" : 11 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38b"), "val" : 12 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38c"), "val" : 13 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38d"), "val" : 14 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38e"), "val" : 15 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38f"), "val" : 16 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e390"), "val" : 17 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e391"), "val" : 18 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e392"), "val" : 19 }
Type "it" for more
> db.pages.find().limit(5)
{ "_id" : ObjectId("5574ca7b192e9dda0925e37f"), "val" : 0 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e380"), "val" : 1 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e381"), "val" : 2 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e382"), "val" : 3 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e383"), "val" : 4 }
可以发现,如果使用 limit方法的话会显示整个集合的所有文档。 指定了 limit 之后, 显示具体的条数,上文中,limit(5) 表示, 显示5条文档。
limit方法除外,还有一个 skip 方法,skip也是接受一个整型的参数,表示查询结果跳过多少个文档。
例如上面插入的30条记录中,我们要显示18-22条记录,那么就应该使用db.pages.find().skip(18).limit(5)
> db.pages.find().skip(18).limit(5)
{ "_id" : ObjectId("5574ca7b192e9dda0925e391"), "val" : 18 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e392"), "val" : 19 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e393"), "val" : 20 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e394"), "val" : 21 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e395"), "val" : 22 }
skip和limit的组合就能做到分页的功能了。但是如果数据量很大,理论上分页就会变得很慢了,比如有一亿条数据,要拿最后一页。那skip的数据量就很多很多了。这样就会变得比较慢。话说回来,有谁会看数据看到最后的几页?正常都是看前面几页数据,所以,skip和limit实现分页是可以接受的。
在mongodb中,如果要对查询结果排序,那么需要使用sort方法。sort方法接收一个文档参数。也就是{key:value}的形式。其中,key表示要排序的字段,value的可取值为 1 / -1 。1表示升序asc,-1表示降序desc。话不多说,直接上例子:
> db.pages.find().sort({val:-1})
{ "_id" : ObjectId("5574ca7b192e9dda0925e39c"), "val" : 29 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e39b"), "val" : 28 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e39a"), "val" : 27 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e399"), "val" : 26 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e398"), "val" : 25 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e397"), "val" : 24 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e396"), "val" : 23 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e395"), "val" : 22 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e394"), "val" : 21 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e393"), "val" : 20 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e392"), "val" : 19 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e391"), "val" : 18 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e390"), "val" : 17 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38f"), "val" : 16 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38e"), "val" : 15 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38d"), "val" : 14 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38c"), "val" : 13 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38b"), "val" : 12 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38a"), "val" : 11 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e389"), "val" : 10 }
Type "it" for more
这个是对val这个key进行逆序排序,所以value取值为-1。 那value值为1的话,就变成升序了。
> db.pages.find().sort({val:1})
{ "_id" : ObjectId("5574ca7b192e9dda0925e37f"), "val" : 0 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e380"), "val" : 1 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e381"), "val" : 2 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e382"), "val" : 3 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e383"), "val" : 4 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e384"), "val" : 5 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e385"), "val" : 6 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e386"), "val" : 7 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e387"), "val" : 8 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e388"), "val" : 9 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e389"), "val" : 10 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38a"), "val" : 11 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38b"), "val" : 12 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38c"), "val" : 13 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38d"), "val" : 14 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38e"), "val" : 15 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e38f"), "val" : 16 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e390"), "val" : 17 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e391"), "val" : 18 }
{ "_id" : ObjectId("5574ca7b192e9dda0925e392"), "val" : 19 }
Type "it" for more
>
那如果要对多个值进行组合排序呢? 就好比如对于我们最初的例子 users 集合。 要对users集合进行排序。其中 按照likes升序, 按照username降序。为了达到我们的效果,我们再往users集合里面插入两条数据
> db.users.insert({username:"mongodb",likes:999})
> db.users.insert({username:"springMVC",likes:888})
下面是运行结果,注意观察 likes为888的两个文档。发现username逆序排序了。至此,说明我们的sort实验成功了。
> db.users.find().sort({likes:1,username:-1}).pretty()
{
"_id" : ObjectId("5574bdf3c705777157a515ac"),
"username" : "spring",
"url" : "webinglin.github.io",
"tags" : [
"spring",
"framework"
],
"likes" : 777,
"author" : "linwenbin"
}
{
"_id" : ObjectId("5574cefa192e9dda0925e39e"),
"username" : "springMVC",
"likes" : 888
}
{
"_id" : ObjectId("5574bdd2c705777157a515ab"),
"username" : "redis",
"url" : "webinglin.github.io",
"tags" : [
"redis",
"database",
"nosql"
],
"likes" : 888,
"author" : "linwenbin"
}
{
"_id" : ObjectId("5574cef5192e9dda0925e39d"),
"username" : "mongodb",
"likes" : 999
}
{
"_id" : ObjectId("5574bdabc705777157a515aa"),
"username" : "mongo",
"url" : "webinglin.github.io",
"tags" : [
"mongodb",
"database",
"nosql"
],
"likes" : 999,
"author" : "linwenbin"
}
>
原作者:webinglin
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。