Incorrect query translation with $elemMatch and $or in Mongoose 8.x #15081
Labels
can't reproduce
Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Stale
Prerequisites
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
}
]
}
}
}
The text was updated successfully, but these errors were encountered: