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

166
Views
mongodb query for object properties inside array inside of an object

Datastructure:

Document:
{
  "projects": {
    "projectName" : String,
    "employees": [String]
  }
}

Now i want to find a document which has a project with name "project1" and an employee "employee1".

When i try to query by only one property from the nested projects object, it seems to be no problem. But as soon as i want also to access the nested array. Queries start to fail.

db.documents.find({'projects': { $elemMatch: { name: "project1", $elemMatch: { employees: 'employee1' } } })

I work with meteor collections, so it looks like this:

Documents.findOne({'projects': { $elemMatch: { name: "project1", $elemMatch: { employees: 'employee1' } } })

I also tried this simple approach:

db.collection.find({
  "projects.projectName": "project1",
  "projects.employees": "employee1"
})

but it didn't work.

EDIT: As J.F. suggested. This Query is correct. But i had a Schema Error. After correcting it, everything worked fine.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use the $in operator https://docs.mongodb.com/manual/reference/operator/query/in/

db.collection.find({
  "projects.projectName": "project1",
  "projects.employees": {$in: ["employee1"]}
})
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!