$lookup when foreign field is an array

ⅰ亾dé卋堺 提交于 2019-12-04 14:53:59

You can use below aggregation

You can use new $lookup syntax to $unwind the foreign array inside the $lookup pipeline and then can easily $match with ids

db.bookings.aggregate([
  { "$lookup": {
    "from": "sports",
    "let": { "slot_id": "$slots.slot_id" },
    "pipeline": [
      { "$unwind": "$slot_divisions" },
      { "$match": { "$expr": { "$in": ["$slot_divisions.div_id", "$$slot_id"] }}},
      { "$project": { "item_name": 1, "item_icon": 1 }}
    ],
    "as": "sports_data"
  }},
  { "$project": {
    "booking_id": { "$arrayElemAt": ["$slots.booking_id", 0] },
    "date": 1,
    "sports_data": 1
  }}
])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!