0

I'm facing a strange issue with Mongoose Update, and I don't know where to search anymore...

I would like to update a document (I want to update because I need to exclude validation) from a model method. This is my code:

module.exports.plugin = function(schema){ schema.add( { deletedAt : { type : Date, default: null } }); schema.methods.softDelete = function(done){ this.update({_id: this._id}, {deletedAt: new Date()}, function(err, num, raw){ logger.info(err, 'Number Updated: ' + num); logger.info(raw); done(err, num); }); }; }; 

The function is correctly triggered, this is the output of the logger:

Number Updated: 1 (ok=true, n=1, updatedExisting=true) 

that made me suppose that everything works, but then (checking directly in mongo) the field deletedAt has not been updated.

Any suggestions?

Thanks in advance

2
  • What does the schema definition look like? Does it have the deletedAt property? Commented Feb 10, 2015 at 14:47
  • Yes, this a plugin that is attached to more than one schema, but the deletedAt proporty id defined here in the plugin with schema.add( { deletedAt : { type : Date, default: null } }); Commented Feb 10, 2015 at 15:01

1 Answer 1

1

When using the instance version of the update method, you don't provide a query conditions parameter as the method already knows to update the current instance.

So your method should look like this instead:

schema.methods.softDelete = function(done){ this.update({$set: {deletedAt: new Date()}}, function(err, num, raw){ logger.info(err, 'Number Updated: ' + num); logger.info(raw); done(err, num); }); }; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.