MongoDB-按方面分组

时间:2019-10-10 07:29:28

标签: mongodb grouping facet

提供了以下收藏

客户

[
    {
      uuid: "first",
      culture: "it-it"
    },
    {
      uuid: "second",
      culture: "de-de"
    }
]

车辆

[
    {
      model: "mymodel",
      users: [
        {
          uuid: "first",
          isOwner: true,
          createdOn: "2019-05-15T06: 00: 00"
        }
      ]
    },
    {
      model: "mymodel",
      users: [
        {
          uuid: "first",
          isOwner: false,
          createdOn: "2019-05-15T06: 00: 00"
        },
        {
          uuid: "second",
          isOwner: true,
          createdOn: "2019-05-15T06: 00: 00"
        }
      ]
    }
]

以及以下查询:

db.customers.aggregate([
  {
    $lookup: {
      from: "vehicles",
      let: {
        uuid: "$uuid"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $in: [
                "$$uuid",
                "$users.uuid"
              ]
            }
          }
        },
        {
          $project: {
            model: 1,
            users: {
              $filter: {
                input: "$users",
                as: "user",
                cond: {
                  $eq: [
                    "$$user.uuid",
                    "$$uuid"
                  ]
                }
              }
            }
          }
        },
        {
          $unwind: "$users"
        },
        {
          $replaceRoot: {
            newRoot: {
              isOwner: "$users.isOwner",
              createdOn: "$users.createdOn"
            }
          }
        }
      ],
      as: "vehicles"
    }
  },
  {
    $facet: {
      "createdOn": [
        {
          $match: {
            "vehicles": {
              $elemMatch: {
                isOwner: true,
                $and: [
                  {
                    "createdOn": {
                      $gte: "2019-05-15T00: 00: 00"
                    }
                  },
                  {
                    "createdOn": {
                      $lt: "2019-05-16T00: 00: 00"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          $project: {
            culture: 1,
            count: {
              $size: "$vehicles"
            }
          }
        },
        {
          $group: {
            _id: 0,
            "total": {
              $sum: "$count"
            }
          }
        }
      ]
    }
  },
  {
    $project: {
      "CreatedOn": {
        $arrayElemAt: [
          "$CreatedOn.total",
          0
        ]
      }
    }
  }
])

我得到以下结果:

[
  {
    "createdOn": 2
  }
]

我想要实现的结果如下:

[
    {
        culture: "it-it",
        results: {
            "createdOn": 1
        }
    },
    {
        culture: "de-de",
        results: {
            "createdOn": 1
        }
    }
]

但是我似乎无法弄清楚可以分组的地方,以便可以得到该结果。 有人可以告诉我这样做的方法吗?

查询越复杂,指标越多,因此这是我所掌握的内容的精简版。

我尝试在所有地方进行分组,但是未能获得想要的结果。

1 个答案:

答案 0 :(得分:0)

以下查询可以为我们提供预期的输出:

db.customers.aggregate([
  {
    $lookup: {
      "from": "vehicles",
      "let": {
        "uuid": "$uuid"
      },
      "pipeline": [
        {
          $unwind: "$users"
        },
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: ["$users.uuid", "$$uuid"]
                },
                {
                  $eq: ["$users.isOwner", true]
                },
                {
                  $gte: ["$users.createdOn", "2019-05-15T00: 00: 00"]
                },
                {
                  $lte: ["$users.createdOn", "2019-05-16T00: 00: 00"]
                }
              ]
            }
          }
        },
        {
          $count:"totalVehicles"
        }
      ],
      as: "vehiclesInfo"
    }
  },
  {
    $unwind: {
      "path": "$vehiclesInfo",
      "preserveNullAndEmptyArrays": true
    }
  },
  {
    $group: {
      "_id": "$culture",
      "culture": {
        $first: "$culture"
      },
      "createdOn": {
        $sum: "$vehiclesInfo.totalVehicles"
      }
    }
  },
  {
    $project: {
      "_id": 0,
      "culture": 1,
      "results.createdOn": "$createdOn"
    }
  }
]).pretty()

输出:

{ "culture" : "de-de", "results" : { "createdOn" : 1 } }
{ "culture" : "it-it", "results" : { "createdOn" : 1 } }