• MongoDB limit 选取 skip跳过 sort排序 方法


    MongoDB  limit 选取 skip跳过 sort排序

    在mysql里有order by  MongoDB用sort代替order by

    > db.user.find()
    { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
    { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }
    { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
    { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }

    Limit 选取 :

    我要从这些 Document 中取出多少个

    选取2条Document

    > db.user.find().limit(2)
    { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
    { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }

    从整个collection集合的第一条数据Document开始选取两条

    Skip 跳过 :

    我要跳过多少个Document

    我要跳过前两个 Document 直接从第三个Document 开始

    > db.user.find().skip(2)
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }
    { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
    { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }

    skip(2) 就是跳过两条Document,从整个collection集合第一条数据Document开始跳过

    Limit + Skip : 

    我只想要第二条和第三条

    > db.user.find()
    { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
    { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }
    { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
    { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }
    > 
    > db.user.find().skip(1).limit(2)
    { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }

    跳过第一条数据,从第二条数据开始选取2条数据Document

    反过来顺序是一样的

    > db.user.find().limit(2).skip(1)
    { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }

    选取两条数据,但要跳过第一条数据,从第二条数据开始选取

    Sort 排序 : 将结果按照关键字排序

    sort写法  {} 里面根据什么Field进行排序 

    1 为升序 , -1 为降序

    db.user.find().sort({ sortField:-1 })

     将find出来的Document 按照 age 进行 升序 | 降序 排列

    升序

    > db.user.find().sort({"age":1})
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }
    { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
    { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
    { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }

    降序

    > db.user.find().sort({"age":-1})
    { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
    { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }
    { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }
    { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }

     Limit + Skip + Sort 混搭:

    选取第二条第三条第四条 并 按照 age 进行 降序排列

    > db.user.find().skip(1).limit(3).sort({age:-1})
    { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }
    { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }
  • 相关阅读:
    Spring IoC详解
    Hibernate 和Mybatis的区别
    Nand Flash 驱动框架
    Nor Flash 驱动框架
    USB驱动框架
    输入子系统框架
    module_init 内核调用过程
    平台设备驱动框架
    LCD驱动框架
    嵌入式-开篇
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/10698074.html
Copyright © 2020-2023  润新知