Easton's Blog Easton's Blog
首页
  • 编程语言

    • Python
  • 框架

    • Django
  • Mdn (opens new window)
  • HTML
  • CSS
  • JavaScript
  • Mysql
  • PostgreSQL
  • Elasticsearch
  • MongoDB
  • Redis
  • 服务器命令
  • Docker
  • GIT
  • 摄影
  • 草稿
  • 归类方式

    • 分类
    • 标签
    • 归档
  • 关于博客

    • 博客教程
    • 友情链接
    • 关于
导航站
GitHub (opens new window)

Easton Yang

爱生活😊爱学习
首页
  • 编程语言

    • Python
  • 框架

    • Django
  • Mdn (opens new window)
  • HTML
  • CSS
  • JavaScript
  • Mysql
  • PostgreSQL
  • Elasticsearch
  • MongoDB
  • Redis
  • 服务器命令
  • Docker
  • GIT
  • 摄影
  • 草稿
  • 归类方式

    • 分类
    • 标签
    • 归档
  • 关于博客

    • 博客教程
    • 友情链接
    • 关于
导航站
GitHub (opens new window)
  • Mysql

  • PostgreSQL

  • ES

  • MongoDB

    • SQL
    • 聚合
      • $project
      • $match
      • $limit
      • $skip
      • $group
      • $sort
      • $unwind
      • $geoNear
  • Redis

  • 数据库
  • MongoDB
eastonyangxu
2023-07-13
目录

聚合

提示

MongoDB 的聚合管道将 MongoDB 文档在一个管道处理完毕后将结果传递给下一个管道处理。管道操作是可以重复的。管道在 Unix 和 Linux 中一般用于将当前命令的输出结果作为下一个命令的参数。

聚合框架中常用的几个操作:

  • $project修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。
  • $match用于过滤数据,只输出符合条件的文档。$match 使用 MongoDB 的标准查询操作。
  • $limit用来限制 MongoDB 聚合管道返回的文档数。
  • $skip在聚合管道中跳过指定数量的文档,并返回余下的文档。
  • $group将集合中的文档分组,可用于统计结果。
  • $sort将输入文档排序后输出。
  • $unwind将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
  • $geoNear输出接近某一地理位置的有序文档。

# $project

类似于 SQL 中的返回指定字段。

    // 只返回age字段
    db.accounts.aggregate([{"$project": {"age": 1, "_id": 0}}])
    
    1
    2
    import pymongo
    db = pymongo.MongoClient("mongodb://localhost:27017/")["ts"]
    print(db.accounts.aggregate([{"$project": {"age": 1, "_id": 0}}]))
    
    1
    2
    3
    // Make sure to add code blocks to your code group

    # $match

    指定各种查询条件,类似于 SQL 中的指定条件查询和$or。

      db.accounts.aggregate([{"$match": {"$or": [{"firstname": "Amber", "lastname" : "Duke"}, {"lastname" : "Davidson"}]}}])
      
      1
      import pymongo
      db = pymongo.MongoClient("mongodb://localhost:27017/")["ts"]
      print(db.accounts.aggregate([{"$match": {"$or": [{"firstname": "Amber", "lastname" : "Duke"}, {"lastname" : "Davidson"}]}}]))
      
      1
      2
      3
      // Make sure to add code blocks to your code group

      # $limit

      指定返回数据条数,类似于 SQL 中的limit 和 pretty。

        db.accounts.aggregate([{"$limit": 1}]).pretty()
        
        1
        import pymongo
        db = pymongo.MongoClient("mongodb://localhost:27017/")["ts"]
        print(db.accounts.aggregate([{"$limit": 1}]))
        
        1
        2
        3
        // Make sure to add code blocks to your code group

        # $skip

        结合 $limit 实现分页,类似于 SQL 中的skip。

        注意

        注意聚合这里 $skip 和 $limit 的顺序,$skip 在 $limit 的前面。

          db.accounts.aggregate([{"$skip": 40}, {"$limit": 10}])
          
          1
          import pymongo
          db = pymongo.MongoClient("mongodb://localhost:27017/")["ts"]
          print(db.accounts.aggregate([{"$skip": 40}, {"$limit": 10}]))
          
          1
          2
          3
          // Make sure to add code blocks to your code group

          # $group

          分组,可以用于统计。

          • $sum计算总和。
          • $avg计算平均值。
          • $min获取集合中所有文档对应值得最小值。
          • $max获取集合中所有文档对应值得最大值。
          • $push将值加入一个数组中,不会判断是否有重复的值。
          • $addToSet将值加入一个数组中,会判断是否有重复的值,若相同的值在数组中已经存在了,则不加入。
          • $first根据资源文档的排序获取第一个文档数据。
          • $last根据资源文档的排序获取最后一个文档数据。
            # 单个字段分组
            db.accounts.aggregate([{"$group": {"_id": "$gender","gender": {"$sum": 1}}}])
            # 多个字段分组
            db.accounts.aggregate([{"$group": {"_id": {"gender": "$gender", "age": "$age"},"gender": {"$sum": 1}}}])
            
            1
            2
            3
            4
            import pymongo
            db = pymongo.MongoClient("mongodb://localhost:27017/")["ts"]
            print(db.accounts.aggregate([{"$group": {"_id": {"gender": "$gender", "age": "$age"},"gender": {"$sum": 1}}}]))
            
            1
            2
            3
            // Make sure to add code blocks to your code group

            # $sort

            排序,-1:降序、1:升序,类似于 SQL 中的sort。

              db.accounts.aggregate([{"$sort": {"age": -1, "balance": 1}}])
              
              1
              import pymongo
              db = pymongo.MongoClient("mongodb://localhost:27017/")["ts"]
              print(db.accounts.aggregate([{"$sort": {"age": -1, "balance": 1}}]))
              
              1
              2
              3
              // Make sure to add code blocks to your code group

              # $unwind

              新增一条数据,用于测试。 db.accounts.insert({"name": "demo", "hobby": ["basketball", "swimming", "排球"]})

                db.accounts.aggregate([{"$match": {"name": "demo"}}, {"$unwind": "$hobby"}])
                
                1
                import pymongo
                db = pymongo.MongoClient("mongodb://localhost:27017/")["ts"]
                print(db.accounts.aggregate([{"$match": {"name": "demo"}}, {"$unwind": "$hobby"}]))
                
                1
                2
                3
                // Make sure to add code blocks to your code group

                # $geoNear

                #MongoDB
                上次更新: 2023/08/08, 20:00:46
                SQL
                基本知识

                ← SQL 基本知识→

                最近更新
                01
                攻略制作要点
                07-18
                02
                摄影主题拍摄
                07-18
                03
                延时摄影剧本
                07-18
                更多文章>
                Theme by Vdoing | Copyright © 2023-2024 Easton Yang | MIT License
                • 跟随系统
                • 浅色模式
                • 深色模式
                • 阅读模式