'MongoDB add dynamic properties in aggregate
I have the following schema structure
[
{
"locale": {
"en": {
"dashboard": {
"DASHBOARD_TITLE": "Some title"
},
"contact": {
"CONTACT_TITLE": "Some title"
}
},
"fr": {
"dashboard": {
"DASHBOARD_TITLE": "Some title french"
},
"contact": {
"CONTACT_TITLE": "Some title FR"
}
},
"pr": {
"dashboard": {
"DASHBOARD_TITLE": "Some title portugues"
},
"contact": {
"CONTACT_TITLE": "Some title PR"
}
}
}
}
]
I have MongoDB script to fetch the values in given format
[
{
"_id": ObjectId("5a934e000102030405000000"),
"locale": {
"fr": {
"contact": {
"CONTACT_TITLE": "Some title FR"
}
}
}
}
]
MongodbScript
function getDataFromMongo(lang, feature) {
await this.staticDataModel.aggregate([
{
"$project": {
locale.fr: { //here fr is the lang param. how to append lang dynamically
"$arrayToObject": {
"$filter": {
"input": {
"$objectToArray": `$locale.${lang}`
},
"as": "el",
"cond": {
$eq: [
"$$el.k",
feature
]
}
}
}
}
}
}
]).exec();
}
What is the solution for appending dynamic properties?
Here locale.${lang} this won't help for me
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
