【问题标题】:Aggregation data design聚合数据设计
【发布时间】:2022-02-10 19:49:40
【问题描述】:

我是 MongoDB 新手,我在表名医生中有数据

[
  {
    _id: "610d0f36a793342c08b49b0a",
    hospitals: [
      {
        _id: "6166c2ea807d823f20722d52",
        hospital: "6166c2ea807d823f20722d4f"
      },
      {
        _id: "61cd446d20c97e302c667e89",
        hospital: "61cd446d20c97e302c667e87"
      }
    ]
  }
]

第二个表名工作日

 [
  {
    _id: "6166c2ea807d823f20722d4f",
    hospitalId: "615442355273d22b90b92491",
    fee: "800"
  },
  {
    _id: "61cd446d20c97e302c667e87",
    hospitalId: "615d4ebc5521472af0aae53d",
    fee: "1000"
  }
]

第三个表名医院

  [
  {
    _id: "615442355273d22b90b92491",
    hospitalName: "ABC"
  },
  {
    _id: "615d4ebc5521472af0aae53d",
    hospitalName: "ABC"
  }
]

如果我使用

db.doctors.find().populate({
          path: "hospitals.hospital",
          populate: {
            path: "hospitalId",
            select: "hospitalName",
          },
        })

我得到这种格式的结果

[
  {
    _id: "610d0f36a793342c08b49b0a",
    hospitals: [
      {
        _id: "6166c2ea807d823f20722d52",
        hospital: {
          _id: "6166c2ea807d823f20722d4f",
          fee: "800",
          hospitalId: {
            _id: "615442355273d22b90b92491",
            hosptialName: "ABC"
          }
        }
      },
      {
        _id: "61cd446d20c97e302c667e89",
        hospital: {
          _id: "61cd446d20c97e302c667e87",
          fee: "1000",
          hospitalId: {
            _id: "615d4ebc5521472af0aae53d",
            hosptialName: "ABC"
          }
        }
      }
    ]
  }
]

现在我正在使用聚合实现这种结果 当我`

db.doctors.aggregate([{$lookup:{from:"weekdays", localField:"hospitals.hospital", foreignField:"_id", as:"hospitals"}}])

返回

[
  {
    _id: "610d0f36a793342c08b49b0a",
    hospitals: [
      {
        _id: "6166c2ea807d823f20722d52",
        hospitalId: "615442355273d22b90b92491",
        fee: "800"
      },
      {
        _id: 61cd446d20c97e302c667e89,
        hospitalId: "615d4ebc5521472af0aae53d",
        fee: 800
      }
    ]
  }
]

如何获得类似于我使用聚合从查找查询中获得的结果。

【问题讨论】:

  • 您可以使用mongoplayground.net 美化您的样本数据。
  • 好的,我已经格式化了数据。请提出解决方案。
  • 不,你展示的不是你会得到的结果,见Mongo Playground请提供有效的样本数据。

标签: node.js mongodb mongoose aggregate mongoose-populate


【解决方案1】:

好的,所以基本上 .populate 是 mongoose 的一种方法。 Populate

我假设您希望聚合的结果如下所示。

[
  {
    "_id": "111",
    "hospitals": [
      {
        "_id": "331",
        "fee": "800",
        "hospital": {
          "_id": "441",
          "hospitalName": "ABC"
        },
        "hospitalId": "441"
      },
      {
        "_id": "332",
        "fee": "1000",
        "hospital": {
          "_id": "442",
          "hospitalName": "ABC"
        },
        "hospitalId": "442"
      }
    ]
  }
]

因此,要实现这一点,您需要 $unwind 数组并再次使用第三个集合 hospitals$unwind 再次执行 $lookup,然后再将它们分组。
这是MongoDB Playground链接

db.doctors.aggregate([
  {
    $lookup: {
      from: "weekdays",
      localField: "hospitals.hospital",
      foreignField: "_id",
      as: "hospitals"
    }
  },
  {
    "$unwind": "$hospitals"
  },
  {
    $lookup: {
      from: "hospitals",
      localField: "hospitals.hospitalId",
      foreignField: "_id",
      as: "hospitals.hospital"
    }
  },
  {
    "$unwind": "$hospitals.hospital"
  },
  {
    "$group": {
      "_id": "$_id",
      "hospitals": {
        "$push": "$hospitals"
      }
    }
  }
])

【讨论】:

    猜你喜欢
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    相关资源
    最近更新 更多