zoukankan      html  css  js  c++  java
  • 学习mongo系列(六)limit(munber),skip(number)

    > db.user.find().pretty()
    {
    "_id" : ObjectId("56946fba3a18f4867aecbcd1"),
    "name" : "admin",
    "password" : "123456",
    "title" : "admin",
    "grade" : 50
    }
    {
    "_id" : ObjectId("5694714a3a18f4867aecbcd2"),
    "name" : "aaaaaaaaa",
    "password" : "123456",
    "grade" : 99
    }
    {
    "_id" : ObjectId("5694ce6685cf2632ea951c05"),
    "name" : "xiaoniao",
    "password" : "lijian",
    "grade" : "15"
    }

    skip()默认的参数是0;

    > db.user.find().limit(2).pretty()
    {
    "_id" : ObjectId("56946fba3a18f4867aecbcd1"),
    "name" : "admin",
    "password" : "123456",
    "title" : "admin",
    "grade" : 50
    }
    {
    "_id" : ObjectId("5694714a3a18f4867aecbcd2"),
    "name" : "aaaaaaaaa",
    "password" : "123456",
    "grade" : 99
    }
    > db.user.find().limit(1).pretty()
    {
    "_id" : ObjectId("56946fba3a18f4867aecbcd1"),
    "name" : "admin",
    "password" : "123456",
    "title" : "admin",
    "grade" : 50
    }
    > db.user.find().limit(1).skip(2).pretty()
    {
    "_id" : ObjectId("5694ce6685cf2632ea951c05"),
    "name" : "xiaoniao",
    "password" : "lijian",
    "grade" : "15"
    }

    插入多条数据方便下边排序:

      

    > db.user.save({name:"张三",password:"zhangsan",grade:60},{name:"李四",password:
    "lisi",grade:75},{name:"毛毛",password:"maoling",grade:99})
    WriteResult({ "nInserted" : 1 })

    发现重大问题,mongo不支持一句插入多条数据,多条数据只能一句一句来插入,所以以上语句分次插入

    > db.user.find().pretty()
    {
    "_id" : ObjectId("56946fba3a18f4867aecbcd1"),
    "name" : "admin",
    "password" : "123456",
    "title" : "admin",
    "grade" : 50
    }
    {
    "_id" : ObjectId("5694714a3a18f4867aecbcd2"),
    "name" : "aaaaaaaaa",
    "password" : "123456",
    "grade" : 99
    }
    {
    "_id" : ObjectId("5694ce6685cf2632ea951c05"),
    "name" : "xiaoniao",
    "password" : "lijian",
    "grade" : "15"
    }
    {
    "_id" : ObjectId("5694d84c85cf2632ea951c06"),
    "name" : "张三",
    "password" : "zhangsan",
    "grade" : 60
    }
    {
    "_id" : ObjectId("5694dab385cf2632ea951c07"),
    "name" : "李四",
    "password" : "lisi",
    "grade" : 80
    }
    {
    "_id" : ObjectId("5694dae885cf2632ea951c08"),
    "name" : "毛毛",
    "password" : "maomao",
    "grade" : 100
    }

    在MongoDB中使用使用sort()方法对数据进行排序,sort()方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而-1是用于降序排列。

    > db.user.find().sort({"grade":-1}).pretty()
    {
    "_id" : ObjectId("5694ce6685cf2632ea951c05"),
    "name" : "xiaoniao",
    "password" : "lijian",
    "grade" : "15"
    }
    {
    "_id" : ObjectId("5694dae885cf2632ea951c08"),
    "name" : "毛毛",
    "password" : "maomao",
    "grade" : 100
    }
    {
    "_id" : ObjectId("5694714a3a18f4867aecbcd2"),
    "name" : "aaaaaaaaa",
    "password" : "123456",
    "grade" : 99
    }
    {
    "_id" : ObjectId("5694dab385cf2632ea951c07"),
    "name" : "李四",
    "password" : "lisi",
    "grade" : 80
    }
    {
    "_id" : ObjectId("5694d84c85cf2632ea951c06"),
    "name" : "张三",
    "password" : "zhangsan",
    "grade" : 60
    }
    {
    "_id" : ObjectId("56946fba3a18f4867aecbcd1"),
    "name" : "admin",
    "password" : "123456",
    "title" : "admin",
    "grade" : 50
    }

  • 相关阅读:
    cocos2dx 3.x(获取当前系统时间)
    cocos2dx 3.x(加载cocostudio进度条)
    cocos2dx 3.x(Button传统按钮)
    cocos2dx 3.x以上(Sprite精灵类的相关属性与创建)
    cocos2dx 3.x版本搭建Mac环境工程(创建一个新的C++工程)百分百可行
    iOS
    iOS -Swift 3.0 -Array(数组与可变数组相关属性及用法)
    iOS -Swift 3.0 -String(字符串常规用法)
    iOS -Swift 3.0 -UIButton属性大全
    iOS -Swift 3.0 -UILabel属性大全
  • 原文地址:https://www.cnblogs.com/mxh1099/p/5125281.html
Copyright © 2011-2022 走看看