What does MongoDB findAndModify do with fields that are in the old document, but not the updated document?

StackOverflow https://stackoverflow.com/questions/19767482

Question

If I call findAndModify, and a document matches the query, but the existing document has fields that the doc does not, will the old fields still exist in the new document, or will they be removed?

I've checked the findAndModify docs and the answers's not obvious. Trying it, fields in the old doc that aren't in the new doc seem to be removed - since this involves losing data, and there's nothing explicit about this is the docs, I'm not sure if this is the expected behaviour or not.

Was it helpful?

Solution

findAndModify behaves exactly like update. This means that the document in the database will be completely replaced by the document you provide, unless you use operators like $set or $push which explicitely modify a document.

By the way: When the node.js-specific documentation is too brief, the description of the analogue command in the general manual is often much more detailed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top