Je hoeft alleen maar een filter toe te voegen in je tweede $addFields
fase die u filtert roomTypes
en de rest van de fasen zou hetzelfde zijn, zojuist de nieuwe code hieronder gemarkeerd vanaf begincommentaar en eindcommentaar,
$reduce
om de lus vanroomDetails.description
te herhalen array $cond om lokaal overeen te komen en matchresultaat terug te sturen naar waarde, hetzelfde proces voorroomDetails.title
array, en voeg deze 2 bijgewerkte velden samen met het huidige object met behulp van$mergeObjects
{
$addFields: {
roomTypes: {
$map: {
input: "$roomTypes",
in: {
$mergeObjects: [
"$$this",
{
Begin:
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"
]
}
}
}
}
]
},
~Einde~
available: {
$reduce: {
input: "$$this.capacity",
initialValue: 0,
in: {
$cond: [
{ $eq: ["$$this.cruiseID", "$cruiseID"] },
"$$this.available",
"$$value"
]
}
}
}
}
]
}
}
}
}
}
In generieke optie heb ik antwoord gegeven in uw referentievraag je kunt dezelfde functie gebruiken zoals,
function languageFilter(inputField, locale) {
return {
$reduce: {
input: inputField,
initialValue: "",
in: {
$cond: [{ $eq: ["$$this.locale", locale] }, "$$this.value", "$$value"]
}
}
};
}
Uw laatste vraag zou zijn:
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
}
}
]);