I have a schema which looks like the following:
{
"...fields"
"profile": {
"past_exams": [
{
"_id": "....",
"details": "..."
},
{
"_id": "....",
"details": "..."
}
]
}
}
and I am trying to select a specific element from the array past_exams but i keep getting an error : MongoServerError: Cannot use $elemMatch projection on a nested field.
Here is my code:
const profile = await this.userModel.findOneAndUpdate({
'profile._id': profileId,
},
updateContent,
{
arrayFilters: [{ "pastExam._id": new Types.ObjectId(pastExamId) }],
new: true,
runValidators: true,
fields: {
"profile.past_exams": {
$elemMatch: {
_id: new Types.ObjectId(pastExamId)
}
}
},
}
);
I am not sure what I am doing wrong. I essentially want the updated element from the array to return to the requester after the query is done.