MongoDB聚合具有链接对象的管道

编程入门 行业动态 更新时间:2024-10-24 18:29:12
本文介绍了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聚合具有链接对象的管道

本文发布于:2023-11-22 20:51:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618974.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管道   对象   链接   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!