How can I compare & update 2 models?

I want to be able to compare 2 models.

Scenario: I have objects that are created from a 3rd party source. I want to periodically check to make sure the information I currently have is up to date. So I would create a new Model (not saved) and would pull up my existing model from my database; Now I would like to compare these two models and if there is a difference between the 3rd party model I have created and the one currently in the database I would like to update the one in my database with the new information.

If possible, the easiest approach would be:

  • save a last_modified timestamp from the original 3rd party source as a last_sync timestamp in your document
  • just compare timestamps

Otherwise you will have to iterate the fields of the two models (excluding those that shouldn't be compared, such as _id) and look for changes to apply.

It may be less time consuming to just update() the whole document, or use $set to be a bit more selective with fields.