MongoDB聚合具有链接对象的管道 [英] MongoDB aggregate pipelines with linked object

查看:56
本文介绍了MongoDB聚合具有链接对象的管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在一个查询中链接两个对象,并对其使用聚合函数.一些数据已本地化,我正在使用此处中的解决方案来获取指定数据区域设置.

I'm linking two objects in one query and use aggregate function for it. Some data is localized and I'm using a solution from here to get data for specified locale.

我正在努力对链接对象(房间)中的数据进行同样的处理.具体来说,请从roomDetails中列出给定语言环境的数据.

I am struggling to do the same with data from the linked object (rooms). Specifically, list data for given locale from the roomDetails.

请查看蒙戈游乐场

推荐答案

您只需要在要过滤 roomTypes 并休息的第二个 $ addFields 阶段添加过滤器阶段是相同的,只是在下面从开始注释和结束注释中突出显示了新代码,

You just need to add filter in your second $addFields stage that you are filtering roomTypes and rest of the stages would be same, just highlighted the new code in below from start comment and end comment,

我建议您在已实现的查询中使用此解决方案,但不确定这样做是否正确,可能会导致查询性能下降.

I am suggesting this solution in your implemented query, i am not sure this is the right approach to do this, might be more will cause the performance of query.

  • $ reduce 迭代 roomDetails.description 数组$ cond的循环以匹配本地并将匹配结果返回给值,对于 roomDetails.title进行相同的处理数组,并使用 $ mergeObjects
  • 将这2个更新的字段与当前对象合并

    • $reduce to iterate loop of roomDetails.description array $cond to match local and return match result to value, same process for roomDetails.title array, and merge this 2 updated fields with current object using $mergeObjects
    •   {
          $addFields: {
            roomTypes: {
              $map: {
                input: "$roomTypes",
                in: {
                  $mergeObjects: [
                    "$$this",
                    {
      

      开始:

                      roomDetails: {
                        $mergeObjects: [
                          "$$this.roomDetails",
                          {
                            description: {
                              $reduce: {
                                input: "$$this.roomDetails.description",
                                initialValue: "",
                                in: {
                                  $cond: [
                                    { $eq: ["$$this.locale", "pl"] },
                                    "$$this.value",
                                    "$$value"
                                  ]
                                }
                              }
                            },
                            title: {
                              $reduce: {
                                input: "$$this.roomDetails.title",
                                initialValue: "",
                                in: {
                                  $cond: [
                                    { $eq: ["$$this.locale", "pl"] },
                                    "$$this.value",
                                    "$$value"
                                  ]
                                }
                              }
                            }
                          }
                        ]
                      },
      

      〜结束〜

                      available: {
                        $reduce: {
                          input: "$$this.capacity",
                          initialValue: 0,
                          in: {
                            $cond: [
                              { $eq: ["$$this.cruiseID", "$cruiseID"] },
                              "$$this.available",
                              "$$value"
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      

      游乐场

      在通用选项中,我已经在您的参考问题中回答了,您可以使用类似的功能,

      In generic option i have answered in your reference question you can use same function like,

      function languageFilter(inputField, locale) {
        return {
          $reduce: {
            input: inputField,
            initialValue: "",
            in: {
              $cond: [{ $eq: ["$$this.locale", locale] }, "$$this.value", "$$value"]
            }
          }
        };
      }
      

      您的最终查询将是:

      let locale = "pl";
      db.cs.aggregate([
        { $match: { cID: "00001" } },
        {
          $lookup: {
            from: "rooms",
            localField: "roomTypes.roomID",
            foreignField: "roomID",
            as: "roomTypes"
          }
        },
        {
          $addFields: {
            title: languageFilter("$title", locale),
            description: languageFilter("$description", locale),
            roomTypes: {
              $map: {
                input: "$roomTypes",
                in: {
                  $mergeObjects: [
                    "$$this",
                    {
                      roomDetails: {
                        $mergeObjects: [
                          "$$this.roomDetails",
                          {
                            description: languageFilter("$$this.roomDetails.description", locale),
                            title: languageFilter("$$this.roomDetails.title", locale)
                          }
                        ]
                      },
                      available: {
                        $reduce: {
                          input: "$$this.capacity",
                          initialValue: 0,
                          in: {
                            $cond: [
                              { $eq: ["$$this.cruiseID", "$cruiseID"] },
                              "$$this.available",
                              "$$value"
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        },
        {
          $project: {
            _id: 0,
            "roomTypes": { _id: 0 },
            "roomTypes.capacity": 0
          }
        }
      ]);
      

      这篇关于MongoDB聚合具有链接对象的管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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