Update by diff?

Is there a convenient way to reduce the size of a payload by only updating with the diff of two objects?

In Javascript I can get the before and after states of an object, I’d like to diff them and only send the update query of that diff.

Hi @Zack_Beyer welcome to the community!

If I don’t misunderstand, you mean to update a document by sending the difference between the old document and the new one instead of having to send the new document as a whole to the server. Is this correct?

You can update part of a document using the update method. See the linked page for extensive behaviour documentation and examples. This update method is also available in all officially supported drivers.

If this is not what you mean, could you provide some examples?

Best regards,
Kevin

This may not be what you are looking for, but there are some libraries out there at apply the JSON Patch protocol to MongoDB. For example: GitHub - eBay/bsonpatch: A BSON implementation of RFC 6902 to compute the difference between two BSON documents

I cannot vouch for this library, I use a proprietary implementation at work, but the layout appears good.

1 Like