汇总多个文档和计数总字段

发布于 2025-02-08 17:23:47 字数 268 浏览 1 评论 0 原文

文档:

{
  _id: "___"
  finish: false or true
  ... some field..
}

汇总结果:

{
  finish: 10,
  non_finish: 3,
  results: [
   docuemnt,
   docuemnt,
   ...
  ]
}

有可能吗?我知道如何计数完成,条件 但是如何汇总文档数组?

Document:

{
  _id: "___"
  finish: false or true
  ... some field..
}

Aggregate result:

{
  finish: 10,
  non_finish: 3,
  results: [
   docuemnt,
   docuemnt,
   ...
  ]
}

Is it possible? I know how can I count finish, with condition
but how to aggregate document array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月流歌 2025-02-15 17:23:47

解决方案1 ​​

  1. $ set - set finish_count non_finish_count 通过检查 finish> finish 字段代码>,如果匹配,则1,else 0。

  2. 。 p> $ group - 组成 null ,总结 finish_count non_finish_count fields。将每个文档添加到文档数组中。

  3. $ unset - 删除 documents.finish_count documents.non_finish_count fields。

db.collection.aggregate([
  {
    $set: {
      finish_count: {
        $cond: {
          if: {
            $eq: [
              "$finish",
              true
            ]
          },
          then: 1,
          else: 0
        }
      },
      non_finish_count: {
        $cond: {
          if: {
            $eq: [
              "$finish",
              false
            ]
          },
          then: 1,
          else: 0
        }
      }
    }
  },
  {
    $group: {
      _id: null,
      finish: {
        $sum: "$finish_count"
      },
      non_finish: {
        $sum: "$non_finish_count"
      },
      documents: {
        $push: "$ROOT"
      }
    }
  },
  {
    $unset: [
      "documents.finish_count",
      "documents.non_finish_count"
    ]
  }
])

示例mongo playground(解决方案1


  1. ) 代码> $ group - null 组组,然后将每个文档添加到文档 array。

  2. $ set - 通过使用 finish $ size )来设置字段>完成:true )和 non_finish 完成:false )来自文档 array。

db.collection.aggregate([
  {
    $group: {
      _id: null,
      documents: {
        $push: "$ROOT"
      }
    }
  },
  {
    $set: {
      finish: {
        $size: {
          $filter: {
            input: "$documents",
            cond: {
              $eq: [
                "$this.finish",
                true
              ]
            }
          }
        }
      },
      non_finish: {
        $size: {
          $filter: {
            input: "$documents",
            cond: {
              $eq: [
                "$this.finish",
                false
              ]
            }
          }
        }
      }
    }
  }
])

示例mongo playground(解决方案2)

Solution 1

  1. $set - Set finish_count and non_finish_count fields by checking the finish, if match then 1, else 0.

  2. $group - Group by null, sum the finish_count and non_finish_count fields. Add each document into documents array.

  3. $unset - Remove documents.finish_count and documents.non_finish_countfields.

db.collection.aggregate([
  {
    $set: {
      finish_count: {
        $cond: {
          if: {
            $eq: [
              "$finish",
              true
            ]
          },
          then: 1,
          else: 0
        }
      },
      non_finish_count: {
        $cond: {
          if: {
            $eq: [
              "$finish",
              false
            ]
          },
          then: 1,
          else: 0
        }
      }
    }
  },
  {
    $group: {
      _id: null,
      finish: {
        $sum: "$finish_count"
      },
      non_finish: {
        $sum: "$non_finish_count"
      },
      documents: {
        $push: "$ROOT"
      }
    }
  },
  {
    $unset: [
      "documents.finish_count",
      "documents.non_finish_count"
    ]
  }
])

Sample Mongo Playground (Solution 1)


Solution 2

  1. $group - Group by null, and add each document into the documents array.

  2. $set - Set the fields by counting the size ($size) of the array by filtering the document with finish (finish: true) and non_finish (finish: false) from the documents array.

db.collection.aggregate([
  {
    $group: {
      _id: null,
      documents: {
        $push: "$ROOT"
      }
    }
  },
  {
    $set: {
      finish: {
        $size: {
          $filter: {
            input: "$documents",
            cond: {
              $eq: [
                "$this.finish",
                true
              ]
            }
          }
        }
      },
      non_finish: {
        $size: {
          $filter: {
            input: "$documents",
            cond: {
              $eq: [
                "$this.finish",
                false
              ]
            }
          }
        }
      }
    }
  }
])

Sample Mongo Playground (Solution 2)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文