MongoDB $查找一个文档的对象数组 [英] MongoDB $lookup on one document's array of object

查看:66
本文介绍了MongoDB $查找一个文档的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在线搜索过,但是找不到与我的情况相符的内容.这是情况.

I have searched online but could not find any match my case. Here is the situation.

我正在使用汇总将一个集合和另一个集合中的一个文档组合在一起

I am using aggregate to combine one collection and one document which is from another collection together

restaurants.aggregate([
  {
    $match: {
      _id: {
        $in: idList
      }
    }
  },
  {
    $lookup: {
      from: "tags",
      localField: "details.restaurantType",
      foreignField: "details.restaurantType._id",
      as: "types"
    }
  },
  {
    $project: {
      restaurantName: "$details.restaurantName",
      restaurantType: "$details.restaurantType",
      type: {
        $filter: {
          input: "$types",
          as: "type",
          cond: {
            $eq: ["$$type._id", "$details.restaurantType"]
          }
        }
      },
      currency: "$details.currency",
      costPerPax: "$details.costPerPax"
    }
  }
]);

当前结果

当前结果中的类型"字段为[],我需要一个匹配的值

current result

The 'type' field in my current result is [], I need a matched value instead

[
    {
        "id": "5c20c7a0036dda80a8baabcc",
        "restaurantName": "Villagio Restaurant Sutera Mall",
        "type": [],
        "currency": "RM",
        "costPerPax": 22,
    },
    {
        "id": "5c20ceb07715216d3c217b7a",
        "restaurantName": "Thai Food Thai Now Sutera Mall",
        "type": [],
        "currency": "RM",
        "costPerPax": 16,
    }
]

预期结果

我需要类型"字段具有这样的另一个集合中的匹配标签名称

expected result

I need the 'type' fields has match tag name from another collection like this

[
    {
        "id": "5c20c7a0036dda80a8baabcc",
        "restaurantName": "Villagio Restaurant Sutera Mall",
        "type": "Western",
        "currency": "RM",
        "costPerPax": 22,
    },
    {
        "id": "5c20ceb07715216d3c217b7a",
        "restaurantName": "Thai Food Thai Now Sutera Mall",
        "type": "Thai",
        "currency": "RM",
        "costPerPax": 16,
    }
]

其他信息

餐厅收藏中的两个文件

Extra Information

two document from restaurants collection

    {
        "details": {
            "restaurantName": "Villagio Restaurant Sutera Mall",
            "restaurantType": "5c01fb57497a896d50f498a8"
        },
        "_id": "5c20c7a0036dda80a8baabcc",
        "status": "OP",
        "__v": 0
    },
    {
        "details": {
            "restaurantName": "Kingshahi Japanese Shop",
            "restaurantType": "5c01fb57497a896d50f49879"
        },
        "_id": "5c20cb4fb7e75180480690c2",
        "status": "OP",
        "__v": 0
    } 

标签收集中的一个文档

        {
            "_id": "5c01fb57497a896d50f49876",
            "details": {
                "restaurantTypeId": "5c01fb57497a896d50f49877",
                "restaurantTypes": [
                    {
                        "_id": "5c01fb57497a896d50f49879",
                        "name": "Asian",
                        "counter": 1
                    },
                    {
                        "_id": "5c01fb57497a896d50f4987a",
                        "name": "Bakery",
                        "counter": 0
                    }
                ]
            }
        }

推荐答案

您可以使用以下优化的聚合管道

You can use below optimised aggregation pipeline

db.restaurants.aggregate([
  { "$lookup": {
    "from": "tags",
    "let": { "restaurantType": "$details.restaurantType" },
    "pipeline": [
      { "$match": {
        "$expr": { "$in": ["$$restaurantType", "$details.restaurantTypes._id"] }
      }},
      { "$unwind": "$details.restaurantTypes" },
      { "$match": {
        "$expr": { "$eq": ["$details.restaurantTypes._id", "$$restaurantType"] }
      }}
    ],
    "as": "types"
  }},
  { "$project": {
    "restaurantName": "$details.restaurantName",
    "restaurantType": "$details.restaurantType",
    "type": { "$arrayElemAt": ["$types.details.restaurantTypes.name", 0] },
    "currency": "$details.currency",
    "costPerPax": "$details.costPerPax"
  }}
])

这篇关于MongoDB $查找一个文档的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆