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 }
    >

  • 相关阅读:
    分布式任务调度 xxl-job
    【线上】 select * from xxoo where 1=1应用挂掉
    【死磕ES】七、基础检索
    【死磕ES】四、基本操作
    【死磕ES】三、基本概念
    【死磕ES】二、搭建环境
    Mac共享文件夹
    微信小程序下拉刷新,上拉加载
    微信小程序textarea输入框出现[object Object]
    微信小程序official-account的使用
  • 原文地址:https://www.cnblogs.com/javasl/p/11279285.html
Copyright © 2011-2022 走看看