-
-
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
$addToSet misbehave - missing docs/information about behavior #11284
Comments
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: []
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const entry = await Test.create({
name: [1,2,3,4,5]
});
console.log(entry);
await Test.findOneAndUpdate({_id: entry._id}, {$addToSet: {name: [6,7,8,9,0]}});
console.log(await Test.find());
}
run(); |
@IslandRhythms - this is exactly the code the reveal the problem - thank you. |
What if there's code mistakenly rely on this misbehavior? |
Right now, the way that Mongoose handles this case is that it adds const mongoose = require('mongoose');
mongoose.set('debug', true);
const testSchema = new mongoose.Schema({
name: [[]] // <-- Array of arrays of any type
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const entry = await Test.create({
name: [1,2,3,4,5]
});
console.log(entry);
await Test.findOneAndUpdate({_id: entry._id}, {$addToSet: {name: [6,7,8,9,0]}});
console.log(await Test.find());
}
run(); We'll avoid adding |
@twhy - i think it will appear as a breaking change and if you choose to update to version 6.2.1 you should handle that since it is obviously a bug. |
@dor-benatia Agree. |
i've stumbled across a weird problem with mongoose $addToSet in patch function,
i am running this code :
when looking at the object in the db i see links: ['a','b',test1', 'test2', 'test3' ] which correspond to $addToSet with $each operator as explained in these docs: https://docs.mongodb.com/manual/reference/operator/update/addToSet/#value-to-add-is-an-array
which should of added this array as single element.
I am seeing in the code the corresponding line that caused that :
lib/types/array/ArrayWrapper.js
line 82.The thing is that it is written no-where in the docs, not even a one liner.
maybe i am missing something ?
mongoose version: 6.0.13
The text was updated successfully, but these errors were encountered: