Skip to content

Commit

Permalink
Merge pull request #11107 from rpenido/fix-castPipelineOperator-$unset
Browse files Browse the repository at this point in the history
fix: allow use type string in $unset update with aggregation pipeline
  • Loading branch information
vkarpov15 authored Dec 20, 2021
2 parents fb1d42d + b71e551 commit a0712da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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

0 comments on commit a0712da

Please sign in to comment.