MongoDB aggregate pipelines with linked object

强颜欢笑 提交于 2021-02-10 05:05:51

问题


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.

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

Please take a look at the Mongo playground


回答1:


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 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",
              {

Start:

                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"
                            ]
                          }
                        }
                      }
                    }
                  ]
                },

~End~

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

Playground


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"]
      }
    }
  };
}

Your final query would be:

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
    }
  }
]);


来源:https://stackoverflow.com/questions/64943723/mongodb-aggregate-pipelines-with-linked-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!