如何使用 MongoDb 聚合 3 个集合的 count/sum?

How to aggregate the count/sum of 3 collections using MongoDb?

我正在尝试汇总多个集合并根据 createdAt 字段获取每日总计:

db={
  "agents": [
    {
      "_id": ObjectId("5a934e000ac2030405000370"),
      "name": "Book Now"
    }
  ],
  "tolls": [
    {
      "_id": ObjectId("5a934e000102030405000000"),
      "amountCollected": 20,
      "plaza": "Shimabala",
      "directionLane": "South/3",
      "cashier": "Chishala",
      "agent": {
        "_id": ObjectId("5a934e000ac2030405000370"),
        "name": "Book Now"
      },
      "createdAt": ISODate("2021-06-02T13:23:06.232Z")
    },
    {
      "_id": ObjectId("a9934ec001a203040500dc00"),
      "amountCollected": 150,
      "plaza": "Shimabala",
      "directionLane": "South/3",
      "cashier": "Chishala",
      "agent": {
        "_id": ObjectId("5a934e000ac2030405000370"),
        "name": "Book Now"
      },
      "createdAt": ISODate("2021-06-04T13:23:06.232Z")
    }
  ],
  "fuel": [
    {
      "_id": ObjectId("60b79e4e4afd77a1c27c730c"),
      "litres": 5.2,
      "price": 90,
      "station": "Chilanga",
      "fuelAttendant": "Manda",
      "agent": {
        "_id": ObjectId("5a934e000ac2030405000370"),
        "name": "Book Now"
      },
      "createdAt": ISODate("2021-06-04T16:06:16.232Z")
    }
  ]
}

预期结果:

[
  {
    "_id": "2021-06-02",
    "fuel": 0,
    "tolls": 1
  },
  {
    "_id": "2021-06-04",
    "fuel": 1,
    "tolls": 1
  }
]

因为这两个集合连接到代理的集合,我有这个查询但不确定如何从那里去:

db.agents.aggregate([
  {
    "$lookup": {
      "localField": "_id",
      "from": "tolls",
      "foreignField": "agent._id",
      "as": "tolls"
    }
  },
  {
    "$unwind": {
      "path": "$tolls",
      "preserveNullAndEmptyArrays": true
    }
  },
  {
    "$lookup": {
      "localField": "_id",
      "from": "fuel",
      "foreignField": "agent._id",
      "as": "fuel"
    }
  },
  {
    "$unwind": {
      "path": "$fuel",
      "preserveNullAndEmptyArrays": true
    }
  } 
])

Link to the Playground

  • 来自 tolls 集合的查询
  • $project 显示必填字段并为“通行费”添加新字段 type
  • $unionWithfuel 集合和 $project 以显示必填字段添加新字段 type 用于“燃料”
  • 现在我们已经在根目录中合并了两个集合文档,并为每个文档添加了 type
  • $group by datetype,计算元素总数
  • $group 仅由 date 构建两个 type 的数组及其 count 的键值格式
  • $addFields 使用 $arrayToObject
  • analytic 字段转换为对象
db.tolls.aggregate([
  { $project: { createdAt: 1, type: "tolls" } },
  {
    $unionWith: {
      coll: "fuel",
      pipeline: [
        { $project: { createdAt: 1, type: "fuel" } }
      ]
    }
  },
  {
    $group: {
      _id: {
        date: {
          $dateToString: {
            date: "$createdAt",
            format: "%Y-%m-%d"
          }
        },
        type: "$type"
      },
      count: { $sum: 1 }
    }
  },
  {
    $group: {
      _id: "$_id.date",
      analytic: {
        $push: { k: "$_id.type", v: "$count" }
      }
    }
  },
  { $addFields: { analytic: { $arrayToObject: "$analytic" } } }
])

Playground

结果伤口为:

[
  {
    "_id": "2021-06-04",
    "analytic": {
      "fuel": 1,
      "tolls": 1
    }
  },
  {
    "_id": "2021-06-02",
    "analytic": {
      "tolls": 1
    }
  }
]

This will not add 0 count field, you have to manage it on frontend/client-side