UPDATE: It is advised not to use the special characters &" in the query. They are meant for updates only.
SOLUTION: Thanks to Gibbs
correct:
const result : any = await mongoose.model('Events').update(
{
_id: eventId,
"groups._id": groupId,
"groups.users._id": userId
},
incorrect:
const result: any = await mongoose.model('Events').findOne(
{
_id: eventId,
"groups._id": "5f270416e7964b20d6f8953e",
"groups.$.users": { _id: '5f270877b4942d2528885dbd' }
})
// OR:
{
_id: eventId,
"groups._id": "5f270416e7964b20d6f8953e",
"groups.$.users._id" : '5f270877b4942d2528885dbd',
})
When using only the first two conditions, (eventId
and groups [$_id]
), it returns the event correctly. However, adding the third condition causes issues.
Does anyone have an idea why this is returning null? The object IDs are directly retrieved from the database. Thank you!
{
"_id": ObjectId("5f270416e7964b20d6f8953d"),
"lastRounds": [],
"name": "EpicFest",
"start": 1596392488896.0,
"end": 1596392500896.0,
"groups": [
{
"_id": ObjectId("5f270416e7964b20d6f8953e"),
"name": "Vossius",
"users": [
{
"created": 1596393590778.0,
"sessionId": null,
"feedBack": {
"messagesSent": 0,
"messagesSentLiked": 0,
"messagesReceived": 0,
"messagesReceivedLiked": 0,
"userFeedbackReceived": [],
"chatFeedbackReceived": [],
"_id": ObjectId("5f270877b4942d2528885dbe")
},
"_id": ObjectId("5f270877b4942d2528885dbd"),
"image": "someAvatr",
"name": "hans",
"groupId": "5f270416e7964b20d6f8953e",
"results": []
},
]
},
{
"_id": ObjectId("5f270416e7964b20d6f8953f"),
"users": [],
"name": "Ignatius"
}
],
"results": [],
"theses": [],
"rounds": 10,
"schedule": [],
"__v": 4
}