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.
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"]}
})