zoukankan      html  css  js  c++  java
  • MongoDB_06_分页查询

    文档的分页查询

    • 统计查询

      统计查询使用count()方法,语法如下:
          db.collection.count(query,options)
      ---------------------------------------------------------
      (1):统计所有记录数字
          统计comment集合的所有记录数字
          db.comment.count()
      
      执行:
      > db.comment.count()
      5
      
      (2):按条件统计记录数
          例如:统计userid为1003的记录条数
          db.comment.count({userid:"1003"})
          
      执行:
      > db.comment.find({userid:"1003"})    --查询
      { "_id" : "3", "articleid" : "100003", "content" : "我爱你,绩憨憨3", "userid" : "1003", "nickname" : "hanhan", "createdatetime" : ISODate("2020-02-28T07:19:45.223Z"), "likenum" : 10, "state" : null }
      > db.comment.count({userid:"1003"})    --统计
      1
      
      
    • 分页列表查询

      可以使用limit()方法来读取指定数量的数据,使用skip()方法来跳过指定数量的数据。
      基本语法:
          db.collection名.find().limit(NUMBER).skip(NUMBER)
          
      (1):如果你想反悔指定条数的记录,可以在find()方法后面调用limit来返回结果(TopN),默认值20,
          db.comment.find().limit(3)
          
      (2):skip方法接受一个参数作为跳过的记录条数,(前N个不要),默认值是0。
          db.comment.find().skip(1)
          
       --------
      分页查询:每页2个,第二页开始,跳过前2条数据,接着值显示第3和4条数据
       //第一页
      db.comment.find().skip(0).limit(2)
      //第二页
      db.comment.find().skip(2).limit(2)
      //第三页
      db.comment.find().skip(4).limit(2)
      
    • 排序查询

      sort()方法对数据进行排序。sort()方法可以通过指定排序的字段,并使用1和-1来指定排序的方式

      基本语法:
          db.collection名:find().sort({KEY:1})      # 1--升序 KEY--文档键
              
      示例:对userid降序排列,likenum升序排列
          db.comment.find().sort({userid:-1,likenum:1})
      
    • 注意:sort()skip(),limit()三个放在一起执行,执行的顺序为sort(),skip(),最后是limit(),和书写顺序无关。

  • 相关阅读:
    Mysql Got a packet bigger than 'max_allowed_packet' bytes
    Git之IDEA集成Git更新项目Update Type选项解释
    IDEA获取GIT仓库时更新类型update type的选择
    git merge和git rebase的区别
    git merge和git merge --no-ff的区别
    Git中fetch和pull命令的区别
    git官网下载太慢解决方法
    IDEA执行Thread.activeCount() = 2的问题
    k8s 常见错误汇总
    Axure9破解
  • 原文地址:https://www.cnblogs.com/zhoujun007/p/12380254.html
Copyright © 2011-2022 走看看