I am trying to query a User collection with multiple nested objects, and I'm not sure how to properly use the projection operators (e.g. $) as they seem to work for arrays not objects.
Each user has a "booksRecords" object, with multiple book objects (e.g. hc_1_3, hc_1_4, etc). Each book object has a field called currLevel and I am trying to find kids that have at least one book object where currLevel: 'nursery'.
I tried doing User.find({'booksRecords.$.currLevel': 'nursery'}), but that doesn't seem to be working and I wonder what is the correct way to query nested objects?
I checked Querying nested in mongoDB, but it is different from my case as I'm querying nested objects.
[
//first object
{
_id: "xxx",
booksRecords: {
hc_1_3: {
markedRead: false,
currLevel: "elementary"
},
hc_1_2: {
markedRead: false,
currLevel: "elementary"
}
}
},
//second object
{
_id: "xyz",
booksRecords: {
hc_1_3: {
markedRead: false,
currLevel: "elementary"
},
hc_1_2: {
markedRead: false,
currLevel: "nursery"
}
}
}
]