-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect query translation with $elemMatch and $or in Mongoose 8.x #15081
Comments
I'm unable to repro, the following script shows that Mongoose correctly casts $or within $elemMatch: it correctly casts const mongoose = require('mongoose');
mongoose.set('debug', true);
// Define the schema and model
const UserDataItemSchema = new mongoose.Schema({
user: mongoose.Schema.Types.ObjectId,
client: mongoose.Schema.Types.ObjectId,
sharedWith: [
{
targetType: String,
targetId: String,
isShared: Boolean,
},
],
});
const UserDataItem = mongoose.model('UserDataItem', UserDataItemSchema);
(async () => {
// Connect to MongoDB
await mongoose.connect('mongodb://localhost:27017/test');
console.log('Connected to MongoDB');
// Clear the collection for a clean slate
await UserDataItem.deleteMany({});
// Insert sample data
await UserDataItem.create([
{
user: new mongoose.Types.ObjectId('6755824f6f0da222862ad889'),
client: new mongoose.Types.ObjectId('6755824f6f0da222862ad855'),
sharedWith: [
{ targetType: 'user', targetId: '6755824f6f0da222862ad889', isShared: true },
{ targetType: 'role', targetId: 'admin', isShared: true },
],
},
]);
// Define the problematic query
const query = {
sharedWith: {
$elemMatch: {
$or: [
{
targetType: 'user',
targetId: '6755824f6f0da222862ad889',
isShared: true,
},
{
targetType: 'role',
targetId: { $in: ['superadmin'] },
isShared: true,
},
],
},
},
};
// Execute the query
const results = await UserDataItem.find(query);
console.log('Query Results:', results);
})(); Output:
|
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
This issue was closed because it has been inactive for 19 days and has been marked as 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: