MongoDB - 如何带年龄段数据

MongoDB - How to bring age group data

如何从 MongoDB 中的集合中提取年龄组基础数据,即 0-18、19-24、25-34、35+ 有多少人

[
  {
    "_id": ObjectId("608be7c608c7de2367c89638"),
    "status": true,
    "gender": "Male",
    "first_name": "Vinter",
    "last_name": "R",
    "dob": "1-2-1999"
  },
  {
    "_id": ObjectId("608be7c608c7de2267c89639"),
    "status": true,
    "gender": "Male",
    "first_name": "Ray",
    "last_name": "Morgan",
    "dob": "1-2-2015"
  }
  ....
]

参观 Mongo 游乐场: https://mongoplayground.net/p/4ydNg9Plh6P

使用$bucket

db.collection.aggregate([
  {
    $bucket: {
      groupBy: {
        "$subtract": [
          {
            $year: new Date()
          },
          {
            $toInt: {
              $substr: [
                "$dob",
                {
                  $subtract: [
                    {
                      $strLenCP: "$dob"
                    },
                    4
                  ]
                },
                4
              ]
            }
          }
        ]
      },
      // Field to group by
      boundaries: [
        0,
        19,
        25,
        35,
        100
      ],
      // Boundaries for the buckets
      default: "Other",
      // Bucket id for documents which do not fall into a bucket
      output: {
        // Output for each bucket
        "count": {
          $sum: 1
        },
        "artists": {
          $push: {
            "name": {
              $concat: [
                "$first_name",
                " ",
                "$last_name"
              ]
            },
            "age": {
              "$subtract": [
                {
                  $year: new Date()
                },
                {
                  $toInt: {
                    $substr: [
                      "$dob",
                      {
                        $subtract: [
                          {
                            $strLenCP: "$dob"
                          },
                          4
                        ]
                      },
                      4
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
])

mongoplayground

有趣的问题!

感谢@Takis 和@YuTing。

  1. 来自@Takis 对 $bucket 的评论的好提示。

  2. @雨婷的回答很好

利用 MongoDB 提供的功能认为这个答案更短。


$toDate - 将日期字符串转换为日期(以上版本 4.0 支持)。

$dateDiff - 日期减法并获取单位(在版本 5 中支持)。

$$CURRENT - 获取当前迭代文档的变量。用于添加到 persons 数组字段(通过 $push)。

$switch - 根据条件显示 group 值(可选)。

db.collection.aggregate([
  {
    "$addFields": {
      "age": {
        $dateDiff: {
          startDate: {
            $toDate: "$dob"
          },
          endDate: "$$NOW",
          unit: "year"
        }
      }
    }
  },
  {
    $bucket: {
      groupBy: "$age",
      // Field to group by
      boundaries: [
        0,
        19,
        25,
        35
      ],
      // Boundaries for the buckets
      default: "Other",
      // Bucket id for documents which do not fall into a bucket
      output: {
        // Output for each bucket
        "count": {
          $sum: 1
        },
        "persons": {
          $push: "$$CURRENT"
        }
      }
    }
  },
  {
    $project: {
      _id: 0,
      group: {
        $switch: {
          branches: [
            {
              case: {
                $lt: [
                  "$_id",
                  19
                ]
              },
              then: "0-18"
            },
            {
              case: {
                $lt: [
                  "$_id",
                  25
                ]
              },
              then: "19-24"
            },
            {
              case: {
                $lt: [
                  "$_id",
                  35
                ]
              },
              then: "25-34"
            }
          ],
          default: "35+"
        }
      },
      count: 1,
      persons: 1
    }
  }
])

Sample Mongo Playground