zoukankan      html  css  js  c++  java
  • (06)mongodb 游标与分页

    遍历1:

    > var mycursor=db.person.find();
    > while(mycursor.hasNext()){printjson(mycursor.next())}
    { "_id" : 1, "name" : "张三", "sex" : "男", "age" : 37.3 }
    { "_id" : 2, "name" : "张三", "sex" : "男", "age" : 27.3 }
    { "_id" : 3, "name" : "张三", "sex" : "男", "age" : 27.3 }
    { "_id" : 4, "name" : "李四", "class" : "三班", "score" : 90 }
    { "_id" : 5, "like" : "football", "name" : "王五" }
    {
    "_id" : ObjectId("5d4190e8c21b717496b2989b"),
    "name" : "lisi",
    "age" : 13,
    "area" : "chaoyang",
    "gender" : "male"
    }
    >

    遍历2:

    > var mycursor=db.person.find();
    > mycursor.forEach(function(obj){printjson(obj)})
    { "_id" : 1, "name" : "张三", "sex" : "男", "age" : 37.3 }
    { "_id" : 2, "name" : "张三", "sex" : "男", "age" : 27.3 }
    { "_id" : 3, "name" : "张三", "sex" : "男", "age" : 27.3 }
    { "_id" : 4, "name" : "李四", "class" : "三班", "score" : 90 }
    { "_id" : 5, "like" : "football", "name" : "王五" }
    {
    "_id" : ObjectId("5d4190e8c21b717496b2989b"),
    "name" : "lisi",
    "age" : 13,
    "area" : "chaoyang",
    "gender" : "male"
    }
    >

    分页:(skip、limit);

    db.person.find().skip((n-1)*m).limit(m); 查询第n页,每一页m条

    > var mycursor=db.person.find().skip((1-1)*2).limit(2);  共两页,第一页
    > while(mycursor.hasNext()){printjson(mycursor.next())}
    { "_id" : 1, "name" : "张三", "sex" : "男", "age" : 37.3 }
    { "_id" : 2, "name" : "张三", "sex" : "男", "age" : 27.3 }
    >
    > var mycursor=db.person.find().skip((2-1)*2).limit(2);  共两页,第二页
    > while(mycursor.hasNext()){printjson(mycursor.next())}
    { "_id" : 3, "name" : "张三", "sex" : "男", "age" : 27.3 }
    { "_id" : 4, "name" : "李四", "class" : "三班", "score" : 90 }
    >

  • 相关阅读:
    GetAwaiter和GetResult
    Sql中的小数点和保留位数
    简单工厂类
    c#种GetType()和TypeOf()的区别
    php 内置正则配置邮箱
    通过手机号获取定位
    使用navicat连接mysql 报错:2003-Can't comment to Mysql server on '192.168.X.X'(10038)
    java基础系列(七):内部类的详解
    bootstrap : 响应式导航
    CSS
  • 原文地址:https://www.cnblogs.com/javasl/p/11279285.html
Copyright © 2011-2022 走看看