1

Documentation says about Model::remove() method:

Removes all documents that match conditions from the collection. To remove just the first document that matches conditions, set the single option to true.

The question is how to set this option?

I've tried:

1) Model.remove({code: 55, single: true }, function (err, deleted) {... — no result, because, as I can expect, single in this case is considered as a field, which doesn't exist;

2) Model.remove({code: 55}, {single: true }, function (err, deleted) {TypeError: callback.apply is not a function.

I'm a Mongoose-beginner, thanks in advance for any help.

Boolean_Type
  • 1,146
  • 3
  • 13
  • 40
  • Note that using the static `remove` doesn't trigger your schema's middleware, i.e. pre('save'...), validate, etc. Are you sure you don't want to find and remove instead? http://mongoosejs.com/docs/middleware.html – Eat at Joes Mar 16 '18 at 23:40
  • Yes, I've read about it and I undestand, that `deleteOne()` is preferred. I just want to know, how to set that this `single` option, if it's possible. – Boolean_Type Mar 16 '18 at 23:42
  • Unfortunatelly, when I run `Model.remove({code: 55}, null, {single: true }, function (err, deleted) {...`, callback even does not trigger... – Boolean_Type Mar 16 '18 at 23:56

1 Answers1

2

Oh my, I've found, how to solve it - in source code:

Model.remove({code: 55}).setOptions({ single: true }).exec(function (err, deleted) {...

Boolean_Type
  • 1,146
  • 3
  • 13
  • 40