MongoDB-查询

任何数据库中查询都是最麻烦的,在MongoDB中对于查询的支持非常到位,有关系运算,逻辑运算,数组运算等等
首先对于数据的操作核心语法:”db.集合名称.find({查询条件} , {设置显示的字段})“

  • 查询一条文档:(查询一条表中数据):

db.dept.findOne()
输出:{
"_id" : ObjectId("5be8eab516d1fcb72bc0dd34"),
"deptno" : 10,
"dname" : "xc",
"loc" : "北京"
}
db.infos.find({'url':'www.hh.cn'})
输出:{ "_id" : ObjectId("5be917a716d1fcb72bc0dd3b"), "url" : "www.hh.cn" }

对于设置的显示字段严格来讲就称为数据的投影操作,(从一条数据的全部字段中拿出部分想要显示的字段来显示出来,就叫投影操作)如果不需要显示的字段设置”0“,而需要显示的设置为‘1’:
比如不想要显示_id:

db.infos.find({'url':'www.hh.cn'},{'_id':0})
或者:
db.infos.find({'url':'www.hh.cn'},{'_id':0,'url':1})

不部分情况下这种投影操作的意义不大。同时对于数据的查询也可以使用‘pretty()’函数进行漂亮的显示。数据列多的时候一定可以看出漂亮的显示。

  • 关系运算查询:
    MongoDB中支持关系查询操作:大于($gt)、小于($lt)、大于等于($gte)、小于等于($lte)、不等于($ne)、等于(key:value)。
    查询name是张三的数据:

db.students.find({'name':'张三'}).pretty()

{
    "_id" : ObjectId("5be9271d16d1fcb72bc0dd41"),
    "name" : "张三",
    "sex" : "男",
    "age" : 19,
    "score" : 89,
    "address" : "西湖区"
}

查询年龄大于19的数据:

db.students.find({'age':{'$gt':19}})
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }
{ "_id" : ObjectId("5be927af16d1fcb72bc0dd45"), "name" : "刘琦", "sex" : "女", "age" : 21, "score" : 0, "address" : "上城区" }
查询姓名不等于王五,同时年龄小于19的数据:
db.students.find({'name':{'$ne':'王五'},'age':{'$lt':19}})
{ "_id" : ObjectId("5be9278a16d1fcb72bc0dd44"), "name" : "赵柳", "sex" : "女", "age" : 17, "score" : 66, "address" : "余杭区" }

逻辑运算查询:
与($and)、或($or)、非($not或者$nor)

查询年龄在19~20岁的数据:
db.students.find({'age':{'$gte':19,'$lte':20}})
{ "_id" : ObjectId("5be9271d16d1fcb72bc0dd41"), "name" : "张三", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区" }
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }

And比较容易,只需要用逗号分隔若干个条件即可。

查询年龄大于19或者成绩大于90分的数据:
db.students.find({'$or':[{'age':{'$gt':19}},{'socre':{'$gt':90}}]})
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }
{ "_id" : ObjectId("5be927af16d1fcb72bc0dd45"), "name" : "刘琦", "sex" : "女", "age" : 21, "score" : 0, "address" : "上城区" }
上面用到了”或“,也可以进行”或“的求反:
db.students.find({'$nor':[{'age':{'$gt':19}},{'socre':{'$gt':90}}]})
{ "_id" : ObjectId("5be9271d16d1fcb72bc0dd41"), "name" : "张三", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区" }
{ "_id" : ObjectId("5be9277416d1fcb72bc0dd43"), "name" : "王五", "sex" : "女", "age" : 18, "score" : 100, "address" : "余杭区" }
{ "_id" : ObjectId("5be9278a16d1fcb72bc0dd44"), "name" : "赵柳", "sex" : "女", "age" : 17, "score" : 66, "address" : "余杭区" }

注意,使用或的时候要用到数组结构。

求模运算:
模的运算使用”$mod“来完成,语法”{$mod:[数字,余数]}“:

db.students.find({'age':{'$mod':[20,0]}})
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }
db.students.find({'age':{'$mod':[20,1]}})
{ "_id" : ObjectId("5be927af16d1fcb72bc0dd45"), "name" : "刘琦", "sex" : "女", "age" : 21, "score" : 0, "address" : "上城区" }

范围查询:
只要是数据库必须存在”$in“(在范围之中)、”$nin“(不在范围之中)
查询姓名是张三王五李思的信息:

db.students.find({'name':{'$in':['张三','李思','王五']}})
{ "_id" : ObjectId("5be9271d16d1fcb72bc0dd41"), "name" : "张三", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区" }
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }
{ "_id" : ObjectId("5be9277416d1fcb72bc0dd43"), "name" : "王五", "sex" : "女", "age" : 18, "score" : 100, "address" : "余杭区" }

不在上面范围之内:

db.students.find({'name':{'$nin':['张三','李思','王五']}})
{ "_id" : ObjectId("5be9278a16d1fcb72bc0dd44"), "name" : "赵柳", "sex" : "女", "age" : 17, "score" : 66, "address" : "余杭区" }
{ "_id" : ObjectId("5be927af16d1fcb72bc0dd45"), "name" : "刘琦", "sex" : "女", "age" : 21, "score" : 0, "address" : "上城区" }

范围的操作也需要用到数组结构。

数组查询:
因为MongoDB是支持数组保存的,所以也需要针对数组的查询。可以使用几个运算符:$all、$size、$slice

  • 保存一部分的数组内容:

db.students.insert({'name':'测试-A','course':['语文','数学','英语']})
WriteResult({ "nInserted" : 1 })
db.students.insert({'name':'测试-B','course':['语文','数学']})
WriteResult({ "nInserted" : 1 })
db.students.insert({'name':'测试-C','course':['语文','数学','英语','科学']})
WriteResult({ "nInserted" : 1 })
db.students.find()
{ "_id" : ObjectId("5be9271d16d1fcb72bc0dd41"), "name" : "张三", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区" }
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }
{ "_id" : ObjectId("5be9277416d1fcb72bc0dd43"), "name" : "王五", "sex" : "女", "age" : 18, "score" : 100, "address" : "余杭区" }
{ "_id" : ObjectId("5be9278a16d1fcb72bc0dd44"), "name" : "赵柳", "sex" : "女", "age" : 17, "score" : 66, "address" : "余杭区" }
{ "_id" : ObjectId("5be927af16d1fcb72bc0dd45"), "name" : "刘琦", "sex" : "女", "age" : 21, "score" : 0, "address" : "上城区" }
{ "_id" : ObjectId("5be93fb416d1fcb72bc0dd46"), "name" : "测试-A", "course" : [ "语文", "数学", "英语" ] }
{ "_id" : ObjectId("5be93fc016d1fcb72bc0dd47"), "name" : "测试-B", "course" : [ "语文", "数学" ] }
{ "_id" : ObjectId("5be93fd616d1fcb72bc0dd48"), "name" : "测试-C", "course" : [ "语文", "数学", "英语", "科学" ] }
{ "_id" : ObjectId("5bea316f16d1fcb72bc0dd49"), "name" : "测试-D", "course" : [ "数学", "英语", "科学" ] }

  • 查询同时参加语文和数学课程的学生:
    现在两个数组内容都需要保存,所以使用”{‘$all’:[内容1,内容2]}”

db.students.find({'course':{'$all':['语文','数学']}})
{ "_id" : ObjectId("5be93fb416d1fcb72bc0dd46"), "name" : "测试-A", "course" : [ "语文", "数学", "英语" ] }
{ "_id" : ObjectId("5be93fc016d1fcb72bc0dd47"), "name" : "测试-B", "course" : [ "语文", "数学" ] }
{ "_id" : ObjectId("5be93fd616d1fcb72bc0dd48"), "name" : "测试-C", "course" : [ "语文", "数学", "英语", "科学" ] }

  • 虽然‘$all’可以用于数组上,但是也可以用于一个数据的匹配,例如查询在拱墅区的学生:
    这两种查询结果是一样的:

db.students.find({'address':{'$all':['拱墅区']}})
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }
db.students.find({'address':'拱墅区'})
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }

  • 既然在集合中保存的是数组信息,那么数组就可以利用索引操作,使用”key.index”的方式来定义索引:
    例如查询数组中第二个内容是英语的学生:(index = 1)

db.students.find({'course.1':'英语'})
{ "_id" : ObjectId("5bea316f16d1fcb72bc0dd49"), "name" : "测试-D", "course" : [ "数学", "英语", "科学" ] }

使用“$size”来进行数量的控制:
例如查询出只参加四门课程的学生:

db.students.find({'course':{'$size':4}})
{ "_id" : ObjectId("5be93fd616d1fcb72bc0dd48"), "name" : "测试-C", "course" : [ "语文", "数学", "英语", "科学" ] }

现在有个问题:只要条件满足,数组的内容就全部显示出来了,如果不想要全部显示呢?

  • 现在希望可以控制数组返回的数量,那么可以用“$slice”进行控制:
    例如查询出名字叫测试-C的学生,并且把他的课程显示控制为2门:

db.students.find({'name':'测试-C'},{'course':{'$slice':2}})
{ "_id" : ObjectId("5be93fd616d1fcb72bc0dd48"), "name" : "测试-C", "course" : [ "语文", "数学" ] }

如果想要控制取后两门,学过js语法的都知道用负号:

db.students.find({'name':'测试-C'},{'course':{'$slice':-2}})
{ "_id" : ObjectId("5be93fd616d1fcb72bc0dd48"), "name" : "测试-C", "course" : [ "英语", "科学" ] }

那么取中间两门课程:

db.students.find({'name':'测试-C'},{'course':{'$slice':[1,2]}})
{ "_id" : ObjectId("5be93fd616d1fcb72bc0dd48"), "name" : "测试-C", "course" : [ "数学", "英语" ] }

这里的1,2表示的是从1index开始返回,返回2count个元素。

  • 嵌套集合查询:
    在mongo数据库中每一个集合数据可以继续保存其他集合的数据,比如学生信息中需要保存家长信息。
    现在有如下数据:
{
    "_id" : ObjectId("5bea488e16d1fcb72bc0dd4a"),
    "name" : "王木木-A",
    "sex" : "男",
    "age" : 19,
    "score" : 89,
    "address" : "西湖区",
    "course" : [
        "语文",
        "数学"
    >],
    "parents" : [
        {
            "name" : "王木木-A-父亲",
            "age" : 55
        },
        {
            "name" : "王木木-A-母亲",
            "age" : 56
        }
    ]
}
{
    "_id" : ObjectId("5bea48ed16d1fcb72bc0dd4b"),
    "name" : "王木木-B",
    "sex" : "男",
    "age" : 19,
    "score" : 89,
    "address" : "西湖区",
    "course" : [
        "语文",
        "英语"
    ],
    "parents" : [
        {
            "name" : "王木木-B-父亲",
            "age" : 51
        },
        {
            "name" : "王木木-B-母亲",
            "age" : 53
        }
    ]
}
  • 这种嵌套的集合数据判断只能通过“$elemMatch”查询。
    例如要查询出父母有人年龄是51岁的同学的信息:

db.students.find({'parents':{'$elemMatch':{'age':51}}})
{ "_id" : ObjectId("5bea48ed16d1fcb72bc0dd4b"), "name" : "王木木-B", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区", "course" : [ "语文", "英语" ], "parents" : [ { "name" : "王木木-B-父亲", "age" : 51 }, { "name" : "王木木-B-母亲", "age" : 53 } ] }

  • 判断某个字段是否存在
    使用“$exists”可以判断某个字段是否存在,如果设置为true表示存在,false为不存在。
    例如查询带有parents字段的数据:

db.students.find({'parents':{'$exists':true}})
{ "_id" : ObjectId("5bea488e16d1fcb72bc0dd4a"), "name" : "王木木-A", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区", "course" : [ "语文", "数学" ], "parents" : [ { "name" : "王木木-A-父亲", "age" : 55 }, { "name" : "王木木-A-母亲", "age" : 56 } ] }
{ "_id" : ObjectId("5bea48ed16d1fcb72bc0dd4b"), "name" : "王木木-B", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区", "course" : [ "语文", "英语" ], "parents" : [ { "name" : "王木木-B-父亲", "age" : 51 }, { "name" : "王木木-B-母亲", "age" : 53 } ] }
db.students.find({'parents':{'$exists':false}})
{ "_id" : ObjectId("5be9271d16d1fcb72bc0dd41"), "name" : "张三", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区" }
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }
{ "_id" : ObjectId("5be9277416d1fcb72bc0dd43"), "name" : "王五", "sex" : "女", "age" : 18, "score" : 100, "address" : "余杭区" }
{ "_id" : ObjectId("5be9278a16d1fcb72bc0dd44"), "name" : "赵柳", "sex" : "女", "age" : 17, "score" : 66, "address" : "余杭区" }
{ "_id" : ObjectId("5be927af16d1fcb72bc0dd45"), "name" : "刘琦", "sex" : "女", "age" : 21, "score" : 0, "address" : "上城区" }
{ "_id" : ObjectId("5be93fb416d1fcb72bc0dd46"), "name" : "测试-A", "course" : [ "语文", "数学", "英语" ] }
{ "_id" : ObjectId("5be93fc016d1fcb72bc0dd47"), "name" : "测试-B", "course" : [ "语文", "数学" ] }
{ "_id" : ObjectId("5be93fd616d1fcb72bc0dd48"), "name" : "测试-C", "course" : [ "语文", "数学", "英语", "科学" ] }
{ "_id" : ObjectId("5bea316f16d1fcb72bc0dd49"), "name" : "测试-D", "course" : [ "数学", "英语", "科学" ] }

可以利用这种查询来过滤操作。

*** 条件过滤:**
传统关系型数据库对于数据的筛选,首先想到的是where,MongoDB中也有“$where”:
例如查询出所有年龄大于19岁的数据:

db.students.find({'$where':'this.age>19'})
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }
{ "_id" : ObjectId("5be927af16d1fcb72bc0dd45"), "name" : "刘琦", "sex" : "女", "age" : 21, "score" : 0, "address" : "上城区" }
还可以更简化写法:
db.students.find('this.age>19')
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }
{ "_id" : ObjectId("5be927af16d1fcb72bc0dd45"), "name" : "刘琦", "sex" : "女", "age" : 21, "score" : 0, "address" : "上城区" }

可以看出对于where是可以简化的。但是这类操作是属于对于每一行信息进行判断,对于数据量大的情况并不方便使用。
上面代码严格来讲是属于编写一个操作函数。

  • 上面只查询了一个条件,如果是多个条件的写法是:

db.students.find({'$and':[{'$where':'this.age>19'},{'$where':'this.age<21'}]})
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }

虽然这种形式可以实现数据查询,但是最大缺点是将在MGO数据库里面保存的BSON数据变为了JS的语法结构,这样的方式不方便实用数据库索引机制

  • 正则运算:
    要想实现模糊查询,必须使用正则表达式,mgDB的正则是与perl兼容的正则表达式形式。
    基础语法:{key:正则标记}
    完整语法:{key:{‘$regex’:正则标记, ‘$options’:选项}}
    对于options主要是设置时正则的信息查询的标记:
    i:忽略字母大小写;
    m:多行查找
    x:空白字符除了被转义的或在字符类中以外的完全被忽略
    s:匹配所有的字符,包括换行内容

    例如查询以“测”字开头的姓名的数据:

db.students.find({'name':/测/})
{ "_id" : ObjectId("5be93fb416d1fcb72bc0dd46"), "name" : "测试-A", "course" : [ "语文", "数学", "英语" ] }
{ "_id" : ObjectId("5be93fc016d1fcb72bc0dd47"), "name" : "测试-B", "course" : [ "语文", "数学" ] }
{ "_id" : ObjectId("5be93fd616d1fcb72bc0dd48"), "name" : "测试-C", "course" : [ "语文", "数学", "英语", "科学" ] }
{ "_id" : ObjectId("5bea316f16d1fcb72bc0dd49"), "name" : "测试-D", "course" : [ "数学", "英语", "科学" ] }

完整写法:

db.students.find({'name':{'$regex':/测/}})
{ "_id" : ObjectId("5be93fb416d1fcb72bc0dd46"), "name" : "测试-A", "course" : [ "语文", "数学", "英语" ] }
{ "_id" : ObjectId("5be93fc016d1fcb72bc0dd47"), "name" : "测试-B", "course" : [ "语文", "数学" ] }
{ "_id" : ObjectId("5be93fd616d1fcb72bc0dd48"), "name" : "测试-C", "course" : [ "语文", "数学", "英语", "科学" ] }
{ "_id" : ObjectId("5bea316f16d1fcb72bc0dd49"), "name" : "测试-D", "course" : [ "数学", "英语", "科学" ] }

  • 上面是用正则查单个数据,现在来用正则查数组数据:

db.students.find({'course':/科/})
{ "_id" : ObjectId("5be93fd616d1fcb72bc0dd48"), "name" : "测试-C", "course" : [ "语文", "数学", "英语", "科学" ] }
{ "_id" : ObjectId("5bea316f16d1fcb72bc0dd49"), "name" : "测试-D", "course" : [ "数学", "英语", "科学" ] }

  • 数据排序
    在MongoDB中数据排序只用sort()函数,可以有两个顺序,升(1)、降(-1)
    例如把学生年龄做个排序:

db.students.find().sort({'age':-1})
{ "_id" : ObjectId("5be927af16d1fcb72bc0dd45"), "name" : "刘琦", "sex" : "女", "age" : 21, "score" : 0, "address" : "上城区" }
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }
{ "_id" : ObjectId("5be9271d16d1fcb72bc0dd41"), "name" : "张三", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区" }
{ "_id" : ObjectId("5bea488e16d1fcb72bc0dd4a"), "name" : "王木木-A", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区", "course" : [ "语文", "数学" ], "parents" : [ { "name" : "王木木-A-父亲", "age" : 55 }, { "name" : "王木木-A-母亲", "age" : 56 } ] }
{ "_id" : ObjectId("5bea48ed16d1fcb72bc0dd4b"), "name" : "王木木-B", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区", "course" : [ "语文", "英语" ], "parents" : [ { "name" : "王木木-B-父亲", "age" : 51 }, { "name" : "王木木-B-母亲", "age" : 53 } ] }
{ "_id" : ObjectId("5be9277416d1fcb72bc0dd43"), "name" : "王五", "sex" : "女", "age" : 18, "score" : 100, "address" : "余杭区" }
{ "_id" : ObjectId("5be9278a16d1fcb72bc0dd44"), "name" : "赵柳", "sex" : "女", "age" : 17, "score" : 66, "address" : "余杭区" }
{ "_id" : ObjectId("5be93fb416d1fcb72bc0dd46"), "name" : "测试-A", "course" : [ "语文", "数学", "英语" ] }
{ "_id" : ObjectId("5be93fc016d1fcb72bc0dd47"), "name" : "测试-B", "course" : [ "语文", "数学" ] }
{ "_id" : ObjectId("5be93fd616d1fcb72bc0dd48"), "name" : "测试-C", "course" : [ "语文", "数学", "英语", "科学" ] }
{ "_id" : ObjectId("5bea316f16d1fcb72bc0dd49"), "name" : "测试-D", "course" : [ "数学", "英语", "科学" ] }

  • 还有一种排序方式叫做自然排序,就是按照数据保存的先后顺序来排序:$natural

db.students.find().sort({'$natural':-1})
{ "_id" : ObjectId("5bea48ed16d1fcb72bc0dd4b"), "name" : "王木木-B", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区", "course" : [ "语文", "英语" ], "parents" : [ { "name" : "王木木-B-父亲", "age" : 51 }, { "name" : "王木木-B-母亲", "age" : 53 } ] }
{ "_id" : ObjectId("5bea488e16d1fcb72bc0dd4a"), "name" : "王木木-A", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区", "course" : [ "语文", "数学" ], "parents" : [ { "name" : "王木木-A-父亲", "age" : 55 }, { "name" : "王木木-A-母亲", "age" : 56 } ] }
{ "_id" : ObjectId("5bea316f16d1fcb72bc0dd49"), "name" : "测试-D", "course" : [ "数学", "英语", "科学" ] }
{ "_id" : ObjectId("5be93fd616d1fcb72bc0dd48"), "name" : "测试-C", "course" : [ "语文", "数学", "英语", "科学" ] }
{ "_id" : ObjectId("5be93fc016d1fcb72bc0dd47"), "name" : "测试-B", "course" : [ "语文", "数学" ] }
{ "_id" : ObjectId("5be93fb416d1fcb72bc0dd46"), "name" : "测试-A", "course" : [ "语文", "数学", "英语" ] }
{ "_id" : ObjectId("5be927af16d1fcb72bc0dd45"), "name" : "刘琦", "sex" : "女", "age" : 21, "score" : 0, "address" : "上城区" }
{ "_id" : ObjectId("5be9278a16d1fcb72bc0dd44"), "name" : "赵柳", "sex" : "女", "age" : 17, "score" : 66, "address" : "余杭区" }
{ "_id" : ObjectId("5be9277416d1fcb72bc0dd43"), "name" : "王五", "sex" : "女", "age" : 18, "score" : 100, "address" : "余杭区" }
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }
{ "_id" : ObjectId("5be9271d16d1fcb72bc0dd41"), "name" : "张三", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区" }

  • 数据分页

skip(n):表示跨过多少数据
limit(n):表示取出的数据行的个数限制

例如:分页显示第一页,每页显示2条:skip(0),limit(2):
》db.students.find().sort({'$natural':1}).skip(0).limit(2)
{ "_id" : ObjectId("5be9271d16d1fcb72bc0dd41"), "name" : "张三", "sex" : "男", "age" : 19, "score" : 89, "address" : "西湖区" }
{ "_id" : ObjectId("5be9274916d1fcb72bc0dd42"), "name" : "李思", "sex" : "男", "age" : 20, "score" : 69, "address" : "拱墅区" }

第二页,每页显示2条:

db.students.find().sort({'$natural':1}).skip(2).limit(2)
{ "_id" : ObjectId("5be9277416d1fcb72bc0dd43"), "name" : "王五", "sex" : "女", "age" : 18, "score" : 100, "address" : "余杭区" }
{ "_id" : ObjectId("5be9278a16d1fcb72bc0dd44"), "name" : "赵柳", "sex" : "女", "age" : 17, "score" : 66, "address" : "余杭区" }

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342

推荐阅读更多精彩内容