Skip to content
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

fix: allow use type string in $unset update with aggregation pipeline #11107

Merged
merged 2 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/helpers/query/castUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {

function castPipelineOperator(op, val) {
if (op === '$unset') {
if (!Array.isArray(val) || val.find(v => typeof v !== 'string')) {
if (typeof val !== 'string' && (!Array.isArray(val) || val.find(v => typeof v !== 'string'))) {
throw new MongooseError('Invalid $unset in pipeline, must be ' +
'an array of strings');
' a string or an array of strings');
}
return val;
}
Expand Down
14 changes: 14 additions & 0 deletions test/model.update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3237,6 +3237,20 @@ describe('model: updateOne: ', function() {
assert.strictEqual(doc.newProp, void 0);
});

it('update pipeline - $unset with string (gh-11106)', async function() {
const schema = Schema({ oldProp: String, newProp: String });
const Model = db.model('Test', schema);

await Model.create({ oldProp: 'test' });
await Model.updateOne({}, [
{ $set: { newProp: 'test2' } },
{ $unset: 'oldProp' }
]);
const doc = await Model.findOne();
assert.equal(doc.newProp, 'test2');
assert.strictEqual(doc.oldProp, void 0);
});

it('update pipeline timestamps (gh-8524)', async function() {
const Cat = db.model('Test', Schema({ name: String }, { timestamps: true }));

Expand Down