Skip to content

Incorrect query translation with $elemMatch and $or in Mongoose 8.x #15081

Closed as not planned
@GalZen

Description

@GalZen

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
}
]
}
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Stalecan't reproduceMongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions