Hi,
This is my first post to this list, sorry if this has already been discussed, but I haven't found it.
When I change a path in a document and save it, if the save operation fails (due to a duplicate key, for example), the path I've modified is now not marked as modified anymore.
This implies that if I try to save again, that path won't be updated and now I have a inconsistent document in memory.
Example:
// Now we're going to finalize order2
// We choose a random code for it
order2.isFinalized = true
order2.code = code
order2.save(function (err) {
// Unfortunately the code is taken (duplicate key error)
assert(err)
assert(err.code === 11001 || err.code === 11000)
// No problem, pick another code and try again
order2.code = code2
order2.save(function (err) {
// This time it's fine
assert.ifError(err)
// Now if we check the DB...
Order.findById(order2, function (err, order2InDB) {
assert.ifError(err)
// save() hasn't saved the isFinalized path
assert(order2.isFinalized)
assert(order2InDB.isFinalized) // this will throw
})
})
})
Full code:
https://gist.github.com/sitegui/dbcbca491cbf91a10ed0I don't know whether this is intended behavior or an actual bug. Can someone confirm this?
Thank you for the support.