在前面的文章里面主要介绍了MongoDB的文档,集合,数据库等操作和对文档的增、删、改相关知识,接下来会总结一点有关查询的相关知识。
在MySQL中,我们知道数据查询是优化的主要内容,读写分离等技术都是可以用来处理数据库查询优化的,足以见数据库查询是每个系统中很重要的一部分,之前介绍了find的简单使用,下面会介绍一些相对比较复杂一点的查询。
一、数据查询
MySQL数据库中主要是用select 结合where子句实现数据的查询,功能特别强大,例如多表联合查询、支持正则表达式等。不在这里做过多的相关介绍。这里主要介绍MongoDB的相关查询,MongoDB中主要用find()实现数据的查询,同时也可以使用一些条件限制。
1.1显示单条数据
在上篇文章中提到了find()的使用,但是每次查询数据,都是查询所有的,显示其中的一部分,可以用it迭代。有时候我们想要查询其中的一条数据,具体操作要根据具体需求实现。
MongoDB 查询数据的语法
db.collection.find(query, projection)
query :可选,使用查询操作符指定查询条件
projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)。可以使用 pretty() 方法以易读的方式来读取数据,,语法格式如下
>db.col.find().pretty()
pretty() 方法以格式化的方式来显示所有文档。
例如:
db.winner.find().pretty() { "_id" : ObjectId("592e7d1caaa464fa8a557e95"), "winne" : 1955 } { "_id" : ObjectId("592e7d1eaaa464fa8a557e96"), "winne" : 1955 } { "_id" : ObjectId("592e7d1faaa464fa8a557e97"), "winne" : 1955 } { "_id" : ObjectId("592e7d1faaa464fa8a557e98"), "winne" : 1955 } { "_id" : ObjectId("592e7d21aaa464fa8a557e99"), "winne" : 1955 } { "_id" : ObjectId("592e7d22aaa464fa8a557e9a"), "winne" : 1955 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec4"), "winne" : 41 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec5"), "winne" : 42 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec6"), "winne" : 43 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec7"), "winne" : 44 }
1、查询某个集合中的所有数据
db.winner.find() { "_id" : ObjectId("592e7d1caaa464fa8a557e95"), "winne" : 1955 } { "_id" : ObjectId("592e7d1eaaa464fa8a557e96"), "winne" : 1955 } { "_id" : ObjectId("592e7d1faaa464fa8a557e97"), "winne" : 1955 } { "_id" : ObjectId("592e7d1faaa464fa8a557e98"), "winne" : 1955 } { "_id" : ObjectId("592e7d21aaa464fa8a557e99"), "winne" : 1955 } { "_id" : ObjectId("592e7d22aaa464fa8a557e9a"), "winne" : 1955 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec4"), "winne" : 41 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec5"), "winne" : 42 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec6"), "winne" : 43 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec7"), "winne" : 44 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec8"), "winne" : 45 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec9"), "winne" : 46 } { "_id" : ObjectId("592e7e14aaa464fa8a557eca"), "winne" : 47 } { "_id" : ObjectId("592e7e14aaa464fa8a557ecb"), "winne" : 48 } { "_id" : ObjectId("592e7e14aaa464fa8a557ecc"), "winne" : 49 } { "_id" : ObjectId("592e7e14aaa464fa8a557ecd"), "winne" : 50 } { "_id" : ObjectId("592e7e14aaa464fa8a557ece"), "winne" : 51 } { "_id" : ObjectId("592e7e14aaa464fa8a557ecf"), "winne" : 52 } { "_id" : ObjectId("592e7e14aaa464fa8a557ed0"), "winne" : 53 } { "_id" : ObjectId("592e7e14aaa464fa8a557ed1"), "winne" : 54 } Type "it" for more
默认显示20条数据,其他数据可以输入it迭代。
2、显示一条数据
find()是输出所有结果,里面可能有些文档内容相同,但是“_id”肯定是不一样的,这时我们可以使用findOne()方法查询,或者可以使用db.winner.find({winne:1955}).limit(1)。
db.winner.find({winne:1955}) { "_id" : ObjectId("592e7d1caaa464fa8a557e95"), "winne" : 1955 } { "_id" : ObjectId("592e7d1eaaa464fa8a557e96"), "winne" : 1955 } { "_id" : ObjectId("592e7d1faaa464fa8a557e97"), "winne" : 1955 } { "_id" : ObjectId("592e7d1faaa464fa8a557e98"), "winne" : 1955 } { "_id" : ObjectId("592e7d21aaa464fa8a557e99"), "winne" : 1955 } { "_id" : ObjectId("592e7d22aaa464fa8a557e9a"), "winne" : 1955 } 假如要查询winner集合中winne=1955的一条数据,而用find()查询出所有的数据,这时就可以使用findOne() db.winner.findOne({winne:1955}) { "_id" : ObjectId("592e7d1caaa464fa8a557e95"), "winne" : 1955 } 或者可以使用 db.winner.find({winne:1955}).limit(1) { "_id" : ObjectId("592e7d1caaa464fa8a557e95"), "winne" : 1955 } 两者的区别 findOne()有点类似MySQL里面的distinct,会返回查询的第一条结果,如果搜索不到想要的数据就会 返回NULL, db.winner.findOne({winne:200888}) null db.winner.find({winne:1955}).limit(1)方法就和MySQL里面的limit是一样的,主要是限制查询结果的条数。
3、查询满足一定条件的数据
在MySQL中查询时,可以结合where以及字段等信息查询数据,而MongoDB中也是可以的,同样可以支持一些条件判断语句。
格式 | 范例 | RDBMS中的类似语句 | |
---|---|---|---|
等于 | {<key>:<value> } | db.col.find({"winne":"1995"}).pretty() | where |
小于 | {<key>:{$lt:<value>}} | db.col.find({" | where |
小于或等于 | {<key>:{$lte:<value>}} | db.col.find({" | where |
大于 | {<key>:{$gt:<value>}} | db.col.find({" | where |
大于或等于 | {<key>:{$gte:<value>}} | db.col.find({" | where |
不等于 | {<key>:{$ne:<value>}} | db.col.find({" | where |
$gt -------- greater than $gte --------- gt equal $lt -------- less than $lte --------- lt equal $ne ----------- not equal 1、查询winner集合中winne<50的相关数据 db.winner.find({winne:{$lt :50}}) { "_id" : ObjectId("592e7e14aaa464fa8a557ec4"), "winne" : 41 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec5"), "winne" : 42 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec6"), "winne" : 43 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec7"), "winne" : 44 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec8"), "winne" : 45 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec9"), "winne" : 46 } { "_id" : ObjectId("592e7e14aaa464fa8a557eca"), "winne" : 47 } { "_id" : ObjectId("592e7e14aaa464fa8a557ecb"), "winne" : 48 } { "_id" : ObjectId("592e7e14aaa464fa8a557ecc"), "winne" : 49 } { "_id" : ObjectId("592e7e17aaa464fa8a557f28"), "winne" : 41 } { "_id" : ObjectId("592e7e17aaa464fa8a557f29"), "winne" : 42 } { "_id" : ObjectId("592e7e17aaa464fa8a557f2a"), "winne" : 43 } { "_id" : ObjectId("592e7e17aaa464fa8a557f2b"), "winne" : 44 } 2、查询winner集合中40=<winne<50的相关数据 db.winner.find({winne:{$gte:40,$lt:45}}) { "_id" : ObjectId("592e7e14aaa464fa8a557ec4"), "winne" : 41 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec5"), "winne" : 42 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec6"), "winne" : 43 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec7"), "winne" : 44 } { "_id" : ObjectId("592e7e17aaa464fa8a557f28"), "winne" : 41 } { "_id" : ObjectId("592e7e17aaa464fa8a557f29"), "winne" : 42 } { "_id" : ObjectId("592e7e17aaa464fa8a557f2a"), "winne" : 43 } { "_id" : ObjectId("592e7e17aaa464fa8a557f2b"), "winne" : 44 } { "_id" : ObjectId("592e7e18aaa464fa8a557f8c"), "winne" : 41 } { "_id" : ObjectId("592e7e18aaa464fa8a557f8d"), "winne" : 42 } { "_id" : ObjectId("592e7e18aaa464fa8a557f8e"), "winne" : 43 } { "_id" : ObjectId("592e7e18aaa464fa8a557f8f"), "winne" : 44 }
4、MongoDB AND 条件
MongoDB 的 find() 方法可以传入多个键(key),每个键(key)以逗号隔开,语法格式如下:
>db.winner.find({key1:value1, key2:value2}).pretty()
#插入测试数据 for(i=0;i<20;i++)db.info2.insert({name:"linux", object:"SA", company:"docker", phone:i}) for(i=0;i<20;i++)db.info2.insert({name:"openstack", object:"DBA", company:"could", phone:i}) #检查测试数据 > db.info2.find() db.info2.find() { "_id" : ObjectId("592f838dd276944818f7edb4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 0 } { "_id" : ObjectId("592f838dd276944818f7edb5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } { "_id" : ObjectId("592f838dd276944818f7edb9"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 5 } { "_id" : ObjectId("592f838dd276944818f7edba"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 6 } { "_id" : ObjectId("592f838dd276944818f7edbb"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 7 } { "_id" : ObjectId("592f838dd276944818f7edbc"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 8 } { "_id" : ObjectId("592f838dd276944818f7edbd"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 9 } { "_id" : ObjectId("592f838dd276944818f7edbe"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 10 } { "_id" : ObjectId("592f838dd276944818f7edbf"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 11 } { "_id" : ObjectId("592f838dd276944818f7edc0"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 12 } { "_id" : ObjectId("592f838dd276944818f7edc1"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 13 } { "_id" : ObjectId("592f838dd276944818f7edc2"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 14 } { "_id" : ObjectId("592f838dd276944818f7edc3"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 15 } { "_id" : ObjectId("592f838dd276944818f7edc4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 16 } { "_id" : ObjectId("592f838dd276944818f7edc5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 17 } { "_id" : ObjectId("592f838dd276944818f7edc6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 18 } { "_id" : ObjectId("592f838dd276944818f7edc7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 19 } >db.info2.find().count() #检查数据的条数 40 #筛选name=linux object=SA phone<5 db.info2.find({name:"linux",object:"SA",phone:{$lt:5}}) 执行 db.info2.find({name:"linux",object:"SA",phone:{$lt:5}}) { "_id" : ObjectId("592f838dd276944818f7edb4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 0 } { "_id" : ObjectId("592f838dd276944818f7edb5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } 筛选name=linux object=SA 5<phone<=10 db.info2.find({name:"linux",object:"SA",phone:{"$gt":5,"$lte":10}}) db.info2.find({name:"linux",object:"SA",phone:{"$gt":5,"$lte":10}}) { "_id" : ObjectId("592f838dd276944818f7edba"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 6 } { "_id" : ObjectId("592f838dd276944818f7edbb"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 7 } { "_id" : ObjectId("592f838dd276944818f7edbc"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 8 } { "_id" : ObjectId("592f838dd276944818f7edbd"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 9 } { "_id" : ObjectId("592f838dd276944818f7edbe"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 10 } 筛选 5<phone<=10 db.info2.find({phone:{"$gt":5,"$lte":10}}) { "_id" : ObjectId("592f838dd276944818f7edba"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 6 } { "_id" : ObjectId("592f838dd276944818f7edbb"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 7 } { "_id" : ObjectId("592f838dd276944818f7edbc"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 8 } { "_id" : ObjectId("592f838dd276944818f7edbd"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 9 } { "_id" : ObjectId("592f838dd276944818f7edbe"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 10 } { "_id" : ObjectId("592f838fd276944818f7edce"), "name" : "openstack", "object" : "DBA", "company" : "could", "phone" : 6 } { "_id" : ObjectId("592f838fd276944818f7edcf"), "name" : "openstack", "object" : "DBA", "company" : "could", "phone" : 7 } { "_id" : ObjectId("592f838fd276944818f7edd0"), "name" : "openstack", "object" : "DBA", "company" : "could", "phone" : 8 } { "_id" : ObjectId("592f838fd276944818f7edd1"), "name" : "openstack", "object" : "DBA", "company" : "could", "phone" : 9 } { "_id" : ObjectId("592f838fd276944818f7edd2"), "name" : "openstack", "object" : "DBA", "company" : "could", "phone" : 10 }
5 MongoDB OR 条件
MongoDB 除了有类似MySQL的AND条件语句外,还有OR 条件语句,OR 条件语句使用了关键字 $or,语法格式如下:
>db.collections.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
#查name=linux 或者object=redis db.info2.find( {$or:[{name:"linux"},{object:"redis"}] } ) db.info2.find(db.info2.find( ... {$or:[{name:"linux"},{object:"redis"}]{$or:[{name:"linux"},{object:"redis"}] ... ... }} ... )) { "_id" : ObjectId("592f838dd276944818f7edb4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 0 } { "_id" : ObjectId("592f838dd276944818f7edb5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } { "_id" : ObjectId("592f838dd276944818f7edb9"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 5 } { "_id" : ObjectId("592f838dd276944818f7edba"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 6 } { "_id" : ObjectId("592f838dd276944818f7edbb"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 7 } { "_id" : ObjectId("592f838dd276944818f7edbc"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 8 } { "_id" : ObjectId("592f838dd276944818f7edbd"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 9 } { "_id" : ObjectId("592f838dd276944818f7edbe"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 10 } { "_id" : ObjectId("592f838dd276944818f7edbf"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 11 } { "_id" : ObjectId("592f838dd276944818f7edc0"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 12 } { "_id" : ObjectId("592f838dd276944818f7edc1"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 13 } { "_id" : ObjectId("592f838dd276944818f7edc2"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 14 } { "_id" : ObjectId("592f838dd276944818f7edc3"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 15 } { "_id" : ObjectId("592f838dd276944818f7edc4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 16 } { "_id" : ObjectId("592f838dd276944818f7edc5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 17 } { "_id" : ObjectId("592f838dd276944818f7edc6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 18 } { "_id" : ObjectId("592f838dd276944818f7edc7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 19 } Type "it" for more > itit { "_id" : ObjectId("592f8964d276944818f7eddc"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 0 } { "_id" : ObjectId("592f8964d276944818f7eddd"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 1 } { "_id" : ObjectId("592f8964d276944818f7edde"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 2 } { "_id" : ObjectId("592f8964d276944818f7eddf"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 3 } { "_id" : ObjectId("592f8964d276944818f7ede0"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 4 } { "_id" : ObjectId("592f8964d276944818f7ede1"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 5 } { "_id" : ObjectId("592f8964d276944818f7ede2"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 6 } { "_id" : ObjectId("592f8964d276944818f7ede3"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 7 } { "_id" : ObjectId("592f8964d276944818f7ede4"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 8 } { "_id" : ObjectId("592f8964d276944818f7ede5"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 9 }
6、AND和OR综合使用
查询phone<5,name=MongoDB或者name=linux
db.info2.find({phone:{$lt:5},$or:[{name:"MongoDB"},{name:"linux"}]}) db.info2.find({phone:{$lt:5},$or:[{name:"MongoDB"},{name:"linux"}]}) { "_id" : ObjectId("592f838dd276944818f7edb4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 0 } { "_id" : ObjectId("592f838dd276944818f7edb5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } { "_id" : ObjectId("592f8964d276944818f7eddc"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 0 } { "_id" : ObjectId("592f8964d276944818f7eddd"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 1 } { "_id" : ObjectId("592f8964d276944818f7edde"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 2 } { "_id" : ObjectId("592f8964d276944818f7eddf"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 3 } { "_id" : ObjectId("592f8964d276944818f7ede0"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 4 }
7、查询结果排序
在MySQL中是有order by条件,可以根据desc或者asc进行升序或者降序操作,而MongoDB中是可以利用sort()方法实现排序的,例如对6中的结果处理,根据phone排序。
基本语法
db.info2.find().sort({phone:1}) #这里phone表示根据该key排序,1表示升序,-1表示降序。
db.info2.find({phone:{$lt:5},$or:[{name:"MongoDB"},{name:"linux"}]}).sort({phone:1}) { "_id" : ObjectId("592f838dd276944818f7edb4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 0 } { "_id" : ObjectId("592f8964d276944818f7eddc"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 0 } { "_id" : ObjectId("592f838dd276944818f7edb5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 1 } { "_id" : ObjectId("592f8964d276944818f7eddd"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f8964d276944818f7edde"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f8964d276944818f7eddf"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } { "_id" : ObjectId("592f8964d276944818f7ede0"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 4 }
8、MongoDB Skip() 方法
在前面介绍了limit(),sort(),count()等方法,接下来要介绍一个比较有趣的skip()方法,在使用limit()的时候可以显示你要求的几条,而skip()方法是跳过几条。
db.info2.find({phone:{$lt:5},$or:[{name:"MongoDB"},{name:"linux"}]}).sort({phone:1}) { "_id" : ObjectId("592f838dd276944818f7edb4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 0 } { "_id" : ObjectId("592f8964d276944818f7eddc"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 0 } { "_id" : ObjectId("592f838dd276944818f7edb5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 1 } { "_id" : ObjectId("592f8964d276944818f7eddd"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f8964d276944818f7edde"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f8964d276944818f7eddf"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } { "_id" : ObjectId("592f8964d276944818f7ede0"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 4 } 使用skip()方法 db.info2.find({phone:{$lt:5},$or:[{name:"MongoDB"},{name:"linux"}]}).sort({phone:1}).skip(3) { "_id" : ObjectId("592f8964d276944818f7eddd"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f8964d276944818f7edde"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f8964d276944818f7eddf"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } { "_id" : ObjectId("592f8964d276944818f7ede0"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 4 } skip 方法有点类似于MySQL里面的limit之间间隔情况。
这里介绍了有关查询的问题,在数据库中,查询是非常重要的一部分,所以介绍的篇幅也是比较多的,后期遇到其他问题也会继续总结输出。
本文出自 “坚持梦想” 博客,请务必保留此出处http://dreamlinux.blog.51cto.com/9079323/1931384
原文地址:http://dreamlinux.blog.51cto.com/9079323/1931384