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

  • 相关阅读:
    游戏开发人员眼中的Unity 3D网页游戏測评报告
    MQTT---HiveMQ源代码具体解释(八)Netty-WebSocket
    RGB 与 (RGB转 YCbCr再转为 RGB)的图像
    Shader的语法
    10种软件开发中 over-engineering 的错误套路
    LeetCode——Min Stack
    nyist 82迷宫寻宝(一)(BFS)
    云计算生态系统
    Linux 查看CPU信息、机器型号等硬件信息
    学习新技术的10个建议
  • 原文地址:https://www.cnblogs.com/javasl/p/11279285.html
Copyright © 2011-2022 走看看