Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

80
Vistas
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

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda