Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
8.8.3
Node.js version
16.20.1
MongoDB server version
7.0
Typescript version (if applicable)
4.6.3
Description
I encountered a bug where a query using .elemMatch() with $or conditions is not translated correctly by Mongoose.
Steps to Reproduce
Run the following query
{
"sharedWith": {
"$elemMatch": {
"$or": [
{ "targetType": "user", "targetId": "someId", "isShared": true },
{ "targetType": "role", "targetId": { "$in": ["role1", "role2"] }, "isShared": true }
]
}
}
}
the query it translated to the following:
The query is translated to:
{
"sharedWith": {
"$ne": { "targetType": "user", "targetId": "someId", "isShared": false },
"$elemMatch": {
"$or": [ {}, {} ]
}
}
}
Expected Behavior
userdataitemslist.find(
{
user: { $ne: ObjectId("6755824f6f0da222862ad889") },
client: ObjectId("6755824f6f0da222862ad855"),
sharedWith: {
$ne: {
targetType: "user",
targetId: "6755824f6f0da222862ad889",
isShared: false
},
$elemMatch: {
$or: [
{
targetType: "user",
targetId: "6755824f6f0da222862ad889",
isShared: true
},
{
targetType: "role",
targetId: { $in: ["admin"] },
isShared: true
}
]
}
}
}