使用查找并对不同的集合进行分组 mongodb

use lookup and group different collection mongodb

您好,我有以下合集

 const TransactionSchema = mongoose.Schema({
    schedule: {
        type: mongoose.Schema.ObjectId,
        required: true,
        ref: "Schedule"
    },
    uniqueCode: {
        type: String,
        required: true
    },
    created: {
        type: Date,
        default: Date.now
    },

    status: {
        type: String,
        required: false
    },
})


    const ScheduleSchema = mongoose.Schema({
    start: {
        type: Date,
        required: true,
    },
    end: {
        type: Date,
        required: false,
    },
    location: {
        type: mongoose.Schema.ObjectId,
        required: true,
        ref: "Location"
    },  

})

我想return计划在交易中出现多少次(其中状态等于'Active')并根据其位置ID对其进行分组,然后查找位置集合以显示名称。 例如我有以下数据。

交易

  [
   {
      "_id":"identifier",
      "schedule":identifier1,
      "uniqueCode":"312312312312",
      "created":"Date",
      "status": 'Active'
   },
   {
      "_id":"identifier",
      "schedule":identifier1,
      "uniqueCode":"1213123123",
      "created":"Date",
      "status": "Deleted"
   }
]

日程安排

[
   {
      "_id":identifier1,
      "start":"date",
      "end":"date",
      "location": id1
   },
   {
      "_id":identifier2,
      "start":"date",
      "end":"date",
      "location": id2
   }
]

我想得到以下结果并将结果限制为 10,并根据其总值对其进行排序:

[
   {
      "locationName":id1 name,
      "total":1
   },
   {
      "locationName":id2 name,
      "total":0
   }
]

谢谢。抱歉我的英语不好。

有点复杂和长的查询。

  1. $lookup - schedule 集合通过匹配加入 transaction 集合:
  • _id (schedule) 与 schedule (transaction)
  • statusActive

和return一个transactions数组。

  1. $lookup - schedule 集合与 location 集合连接到 return location 数组。

  2. $set - 获取 location 数组中的第一个文档,因此该字段将是文档字段而不是数组。 [这需要帮助进一步的阶段]

  3. $group - 按 location._id 分组。并且需要 locationtotal.

    等字段
  4. $sort - 按 total DESC.

    排序
  5. $limit - 限制为 10 个文档 returned。

  6. $project - 修饰输出文档。

db.schedule.aggregate([
  {
    $lookup: {
      from: "transaction",
      let: {
        scheduleId: "$_id"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$schedule",
                    "$$scheduleId"
                  ]
                },
                {
                  $eq: [
                    "$status",
                    "Active"
                  ]
                }
              ]
            }
          }
        }
      ],
      as: "transactions"
    }
  },
  {
    $lookup: {
      from: "location",
      localField: "location",
      foreignField: "_id",
      as: "location"
    }
  },
  {
    $set: {
      location: {
        $first: "$location"
      }
    }
  },
  {
    $group: {
      _id: "$location._id",
      location: {
        $first: "$location"
      },
      total: {
        $sum: {
          $size: "$transactions"
        }
      }
    }
  },
  {
    $sort: {
      "total": -1
    }
  },
  {
    $limit: 10
  },
  {
    $project: {
      _id: 0,
      locationName: "$location.name",
      total: 1
    }
  }
])

Sample Mongo Playground