Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose No matching document found using id() method. Error caused by asynchronous delete requests

Making asynchronous requests in a loop to delete documents from an embedded collection:

_.each deletedItem, (item) ->
     item.$delete()

Erratically throws this error:

{ message: 'No matching document found.', name: 'VersionError' }

When executing:

var resume = account.resumes.id(id);

resume.remove();

account.save(function (err, acct) {
    console.log(err);
    if(err) return next(err);
    res.send(resume);
});

After logging account.resumes and looking through the _id's of all of the resumes, the document I am attempting to find by id, exists in the collection.

530e57a7503d421eb8daca65 FIND:

{ title: 'gggff', _id: 530e57a7503d421eb8daca65 }

IN:

[{ title: 'asddas', _id: 530e57a7503d421eb8daca61 }
{ title: 'gggff', _id: 530e57a7503d421eb8daca65 }
{ title: 'ewrs', _id: 530e57a7503d421eb8daca64 }]

I assume this has to do with the fact that I am performing these requests asynchronously, or that there is a versioning issue, but I have no idea how to resolve it.

It doesn't make any sense to me how when I log the resumes, I can see the resume I attempt to find, yet if I log:

log(account.resumes.id(id));

I get undefined.

UPDATE

I've discovered that my issue is with versioning.

http://aaronheckmann.blogspot.com/2012/06/mongoose-v3-part-1-versioning.html

But I am still unsure how to resolve it without disabling versioning, which I don't want to do.

like image 928
TaylorMac Avatar asked May 04 '26 04:05

TaylorMac


1 Answers

In mongodb version 3, documents now have an increment() method which manually forces incrementation of the document version. This is also used internally whenever an operation on an array potentially alters array element position. These operations are:

$pull $pullAll $pop $set of an entire array

changing the version key

The version key is customizable by passing the versionKey option to the Schema constructor:

new Schema({ .. }, { versionKey: 'myVersionKey' });

Or by setting the option directly:

schema.set('versionKey', 'myVersionKey');

disabling

If you don’t want to use versioning in your schema you can disable it by passing false for the versionKey option.

schema.set('versionKey', false);
like image 60
Enki Avatar answered May 05 '26 18:05

Enki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!