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

  • 相关阅读:
    ASP.NET(4):多语言的解决方案
    无题
    SIP 计时器的总结(转)
    一个Sip协议栈的实现方案
    通过拦截WCF消息进行身份栈传播
    从WPF控件拖放到Winform控件的注意事项
    一个用C#操作OpenLDAP的例子
    通过定制行为拦截WCF消息
    一个基于Prism的方案的介绍
    MVVM模式下附加属性的使用
  • 原文地址:https://www.cnblogs.com/javasl/p/11279285.html
Copyright © 2011-2022 走看看