Mongoose: Does document.remove actually return the removed doc?

Created on 5 Aug 2013  路  12Comments  路  Source: Automattic/mongoose

The documentation suggests that docInstance.remove() takes a callback and passes err, document, but that doesn't seem to actually work:

https://github.com/LearnBoost/mongoose/blob/049288f7b2f2cc0fb4a4a275c7e801304df93938/lib/model.js#L550

remove() calls back with no second argument, even when a document has been removed. It would be nice if the removed document were passed back as in the example, but if not then the documentation should at least be updated with the real behavior.

docs

Most helpful comment

@stieg findOneAndRemove() should return the removed doc, if any. If this isn't working for you please open up a new issue with code samples.

All 12 comments

The code looks ok, when I get to the airport I'll test. Anything special to reproduce?

I see the same issue in 3.6.15. There is nothing special to reproduce it. If you take the example given at http://mongoosejs.com/docs/api.html#model_Model-remove, the product comes through to the callback as undefined.

Commit a82af01cde0e5d92b33aaa571d7eb2b65f1bb625 fixes the issue, but this must be from after 3.6.15.

According to npm, 3.6.15 was release after that commit.

can't reproduce

@aheckmann - can you check the lib/model.js file the installation you used to try to reproduce the problem? Does it match the diff I linked? The version on my system does not contain the argument, whereas the current code does. The package.json in my installation says 3.6.15. I hot-fixed my installed version to include self in that call, and then it started working as expected. I reverted the hotfix, since I don't want to code my app against a hotfix of a library.

I'm on 3.6.15 as well.

The project I'm seeing this on is pretty large; I'll put the relevant code here but I'll also try to reproduce with something small and simple:

imageSchema.statics.destroy = destroyImage;

// ...

  function destroyImage(user, owner, name, done) {
    this.findOne({ owner: getId(owner), name: name }).exec(onFind);

    function onFind(err, image) {
      if (err) return done(err);
      if (!image) return done(app.errors[404]);
      if (!image.hasPermissions(user, 'delete')) return done(app.errors[403]);
      image.remove(onRemove);

      function onRemove(err) {
        return done(err, image);
      }
    }
  }

(I'm using a closure here to imitate the behavior I thought remove() would be providing. Without the closure my second argument was always undefined)

Simple case:

var mongoose = require('mongoose')

console.log('mongoose version:', mongoose.version);

mongoose.connect('mongodb://localhost/testremove');

var schema = mongoose.Schema({
  name: String
});

var Model = mongoose.model('Thing', schema);
var instance = new Model({ name: 'Foo' });

instance.save(onSave);

function onSave(err, doc) {
  console.log('saved:', doc);
  doc.remove(onRemove);
}

function onRemove(err, doc) {
  console.log('removed:', doc);
}
$ node mongoosetest
mongoose version: 3.6.15
saved: { __v: 0, name: 'Foo', _id: 51ffe099e6ba5c7b08000001 }
removed: undefined

Ok yes. I tested against 3.7. The docs for 3.6 are incorrect.

updated the docs.

Is the same true for Model.findOneAndRemove? I just tried this and am getting nothing returned as the doc to the function callback even though the docs state

Finds a matching document, removes it, passing the found document (if any) to the callback.

I am seeing an item removed from my DB. Mongoose 4.8.3, MongoDB 3.4. If so please update the docs there as well. Probably also affects findByIdAndRemove too. TIA.

@stieg findOneAndRemove() should return the removed doc, if any. If this isn't working for you please open up a new issue with code samples.

@stieg findOneAndRemove() should return the removed doc, if any. If this isn't working for you please open up a new issue with code samples.

Will do.

Was this page helpful?
0 / 5 - 0 ratings