How do I create a model instance from object and let Mongoose know it already exists? I'm loading data from cache, so it is always up to date.
I've tried using
var instance = new SomeModel({ '_id': '...', field1: '...', field2: '...' });
but this way Mongoose thinks it's a new object to be inserted, not already existing one. Calling instance.modifiedPaths()
returns ['_id', 'field1', 'field2']
. So if I call instance.save()
it will try to insert it again and throw a duplicate key error.
What's the proper way to do it?