Skip to content

How to clone a document instance #10788

Answered by vkarpov15
saveman71 asked this question in Q&A
Discussion options

You must be logged in to vote

There are a couple of possibilities that may work depending on your use case.

Easiest way would be to just do const clonedDoc = new MyModel(...doc.toObject()) or const clonedDoc = MyModel.hydrate(doc.toObject()), which gets you a new document with all the same properties as doc. You can then depopulate and do whatever you need to.

The downside is that you lose change tracking, so clonedDoc wouldn't be aware of what paths in doc are modified. But if you don't intend to do clonedDoc.save(), this could work.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@saveman71
Comment options

@vkarpov15
Comment options

@saveman71
Comment options

Answer selected by saveman71
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants