2

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
  • 1
    Since you asked that question, and it is a solution, why not accepting it? I'll take a look Commented Nov 21, 2020 at 13:54

1 Answer 1

2

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
    }
  }
]);
Sign up to request clarification or add additional context in comments.

2 Comments

I am struggling to apply your solution in a different place. Would you be se kind to take a look at this mongo playground - mongoplayground.net/p/b_ZMf5KjmVK - and suggest a solution?
are you looking for this playground

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.