I am using mongodb
to insert and update some documents in some collection. When I update a document, I want to get the modified fields only. I can get the complete new document (including the modified fields) by using the returnOriginal
option for updates, but it gives me all the properties.
From the docs:
returnOriginal | optional | When false, returns the updated document rather than the original. The default is true
My code is:
let result = await db.myCollection.findOneAndUpdate(
{_id: ObjectId('5a2451399054bf000453c332')},
data,
{returnOriginal: true}
);
where data
can be either one field, or multiple fields, for example:
let data = {field1: 'newValue1'};
or
let data = {field1: 'newValue1', field2: 'newValue2'};
I would want to get those fields only, but the result of the update is giving me the entire object, including the _id property and all others, even if they weren't modified.
What I'm looking for is an easy way (a mongodb function or property would be ideal) to get only the modified fields, if there is such a way, rather than comparing both objects (before and after).
Using mongodb node driver:
"mongodb": "^3.1.6"