Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!