Answer the question
In order to leave comments, you need to log in
Is it possible to substitute a variable in MongoDB aggregation?
There is a collection Languages
[
{
"code": "ru",
"name": {
"ru": "Русский",
"en": "Russian"
}
},
{
"code": "en",
"name": {
"ru": "Английский",
"en": "English"
}
}
]
[
{
"code": "ru",
"name": ''Русский'
},
{
"code": "en",
"name": "English"
}
]
const $project = {
name: {
$switch: {
branches: [
{
case: { $eq: [ '$code', 'en' ] },
then: '$name.en'
},
{
case: { $eq: [ '$code', 'ru' ] },
then: '$name.ru'
}
]
}
},
code: '$code'
}
const pipeline = [ { $project } ]
const result = await LanguagesModel.aggregate(pipeline)
Answer the question
In order to leave comments, you need to log in
class Language {
code: 'ru' | 'en';
@Transform(prop => ({code: prop.obj.code, name: prop.value[prop.obj.code]}))
name: { en: string, ru: string }
}
const languages = await LanguagesModel.find();
const result = plainToInstance(Language, languages)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question