问题
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 ofroomDetails.description
array $cond to match local and return match result to value, same process forroomDetails.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