在前面的文章里面主要介绍了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 winne = '50'
小于 {<key>:{$lt:<value>}} db.col.find({"winne":{$lt:50}}).pretty() where winne < 50
小于或等于 {<key>:{$lte:<value>}} db.col.find({"winne":{$lte:50}}).pretty() where winne <= 50
大于 {<key>:{$gt:<value>}} db.col.find({"winne":{$gt:50}}).pretty() where winne > 50
大于或等于 {<key>:{$gte:<value>}} db.col.find({"winne":{$gte:50}}).pretty() where winne >= 50
不等于 {<key>:{$ne:<value>}} db.col.find({"winne":{$ne:50}}).pretty() where winne != 50
$gt -------- greater than   $gte --------- gt equal
$lt -------- less than      $lte --------- lt equal
$ne ----------- not equal1、查询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之间间隔情况。

这里介绍了有关查询的问题,在数据库中,查询是非常重要的一部分,所以介绍的篇幅也是比较多的,后期遇到其他问题也会继续总结输出。

转载于:https://blog.51cto.com/dreamlinux/1931384

Mongodb基础实践(二)相关推荐

  1. 缓冲区溢出基础实践(二)——ROP 与 hijack GOT

    3.ROP ROP 即 Return Oritented Programming ,其主要思想是在栈缓冲区溢出的基础上,通过程序和库函数中已有的小片段(gadgets)构造一组串联的指令序列,形成攻击 ...

  2. java grpc 客户端处理 go 服务端多返回值_grpc基础实践(二)

    在此篇中我们将简要介绍关于grpc对java客户端的实现. 在开始开发前,我们需要先导入 io.grpc grpc-netty 1.11.0io.grpc grpc-protobuf 1.11.0io ...

  3. ROS理论与实践——二、ROS基础

    ROS理论与实践--二.ROS基础 前言 一.创建工作空间 1 什么是工作空间 2 创建流程 二.创建功能包 1 创建命令 2 创建流程 三.ROS通信编程 1 话题编程 1.1 话题编程流程 1.2 ...

  4. 零基础实践深度学习之数学基础

    零基础实践深度学习之数学基础 深度学习常用数学知识 数学基础知识 高等数学 线性代数 行列式 矩阵 向量 线性方程组 矩阵的特征值和特征向量 二次型 概率论和数理统计 随机事件和概率 随机变量及其概率 ...

  5. 20145227鄢曼君《网络对抗》Web安全基础实践

    20145227鄢曼君<网络对抗>Web安全基础实践 实验后回答问题 1.SQL注入攻击原理,如何防御? SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是 ...

  6. 20155202《网络对抗》Exp9 web安全基础实践

    20155202<网络对抗>Exp9 web安全基础实践 实验前回答问题 (1)SQL注入攻击原理,如何防御 SQL注入产生的原因,和栈溢出.XSS等很多其他的攻击方法类似,就是未经检查或 ...

  7. 东北农业大学大学计算机基础作业答案,大学计算机基础实践教学改革的研究

    针对目前大学计算机基础实践教学中存在的问题,提出了教学内容.教学方法.教学模式.考试形式等方面的改革方案,并在实践中不断践行和完善,达到预期的教学效果,为提高实践教学质量提供参考. 第 o第. 卷期 ...

  8. Linux课程实践一:Linux基础实践(SSH)

    一.SSH服务 1. 安装SSH (1)查看是否已经安装过ssh服务 rpm -qa |grep ssh (2)进行安装 sudo apt-get install openssh-server Ubu ...

  9. Asp.NetCore程序发布到CentOs(含安装部署netcore)--最佳实践(二)

    原文:Asp.NetCore程序发布到CentOs(含安装部署netcore)--最佳实践(二) Asp.NetCore程序发布到CentOs(含安装部署netcore)--最佳实践(一) 接上一篇 ...

最新文章

  1. Oracle定时执行存储过程
  2. Python再获年度编程语言,微软或成最大赢家
  3. 安卓开发:关于适配的问题
  4. ACE Lock类介绍
  5. 贪吃蛇程序设计报告python_20192116 2019-2020-2 《Python程序设计》实验四报告
  6. typescript利用接口类型声明变量_TypeScript入门指南(基础篇)
  7. PC智能自媒体高效运营管理工具
  8. Android零基础入门第86节:探究Fragment生命周期
  9. XenApp / XenDesktop 7.6 初体验一   安装, 配置站点和序列号服务器
  10. Mac平台的MySQL管理工具
  11. 2020 IDEA插件无法安装问题
  12. cad卸载_CAD一键卸载工具
  13. 网站一键分享到新浪微博QQ空间腾讯微博
  14. linux操作系统是著名的分布式系统,紫光展锐操作系统生态覆盖 Android、Linux、RTOS,面向分布式、智能化...
  15. 虚拟机kali挂主机代理
  16. 计算机其他图标删除,我的电脑其他图标删除方法介绍
  17. T a(v);和T a = v;的区别
  18. 0005 键盘打字如何练成像黑客一样的飞速
  19. 正交设计(收集大牛的文章)
  20. mysql 删除的三种方法_mysql 删除表数据的三种方法

热门文章

  1. Winform 打开下载的文件
  2. ReactOS 0.4.11 发布,Windows 系统的开源替代方案
  3. Android Binder的使用
  4. 2017-2018-2 165X 『Java程序设计』课程 助教总结
  5. 美媒看衰马斯克超级高铁:纽约到华盛顿挖隧道要挖100年
  6. TCP 连接断连问题剖析
  7. Codeforces Round #354 (Div. 2)-A
  8. Swift和OC文件间的相互调用
  9. java编写提升性能的代码
  10. MyBatis第二天