Wrong Options Used in Model.$__handleSave #8739
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
has repro script
There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
When you try to save a document that has no changes, mongoose will run a
Model.exists()
query to make sure the document is in the DB (I don't know why this is done, but that's a different story). That query is done with a subset of the options that were passed toDocument.save()
call, not the original options. This breaks my middleware becauseModel.exists()
triggers aDocument.findOne()
middleware, but the extra options that I have passed in are not sent with this new query.The relevant line is 316 in lib/model.js.
saveOptions
there is a stripped down version of the originaloptions
passed in. I thinkoptions
should be used there, or something similar to it.If the current behavior is a bug, please provide the steps to reproduce.
This seems like a far fetched case. But I ran into this by having a
Document.save()
call inside asession.withTransaction()
call.withTransaction
would then rerun the function after aTransientTransactionError
, triggering this edge case where the document is saved twice.What is the expected behavior?
The custom options should be passed through to the
Model.exists()
method.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: 12.14.0
Mongoose: 5.9.6
MongoDB: 4.2.3
The text was updated successfully, but these errors were encountered: