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

  • 相关阅读:
    Linux 磁盘管理
    03.线程的通知notify与等待wait
    02.线程的等待与中断
    01.线程的三种创建方式与运行
    java--ArrayList,LinkedList应用比较
    java--字符串拼接比较
    java--CharAt,StartWith
    java--split,index,StringTokenizer比较
    java--substring内存溢出问题
    java--String intern
  • 原文地址:https://www.cnblogs.com/javasl/p/11279285.html
Copyright © 2011-2022 走看看