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

128
Vistas
How to query nested objects

MongoDB I have one document, I need to get one object, if locale.en then it will return that info if locale.fr' then it will return that info

{
   "locale": {
        "en": {
          "dashboard": {
            "DASHBOARD_TITLE": "Some title"
          }
        },
        "fr": {
          "dashboard": {
            "DASHBOARD_TITLE": "Some title french"
          }
        },
        "pr": {
          "dashboard": {
            "DASHBOARD_TITLE": "Some title portugues"
          }
        }
      }
    }

How to query this specific object?

db.collectionName.find({locale.en})

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can achieve this using aggregation

Assuming your object has key locale like this

{
"_id": <Some Object Id>,
"locale": {
  "en": {
    "dashboard": {
      "DASHBOARD_TITLE": "Some title"
    }
  },
  "fr": {
    "dashboard": {
      "DASHBOARD_TITLE": "Some title french"
    }
  },
  "pr": {
    "dashboard": {
      "DASHBOARD_TITLE": "Some title portugues"
    }
  }
 }
}

Then aggregation would be

[{
"$project": {
  "locale": {
    "$arrayToObject": {
      "$filter": {
        "input": {
          "$objectToArray": "$locale"
        },
        "as": "el",
        "cond": {
          $eq: [
            "$$el.k",
            "en"
          ]
        }
      }
    }
  }
 }
}]

Just pass your locale in $eq array as second value, I've passed en you can use variable here to build query

Playground: https://mongoplayground.net/p/7ZHIUx_j1GY

Answer on this question explains the pipeline in detail MongoDB projection on specific nested properties

Playground: https://mongoplayground.net/p/wuv4axIMzCc

Dynamic query : https://mongoplayground.net/p/5ouZif-MIry

about 4 years ago · Juan Pablo Isaza Denunciar
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