Convert stringified object to Mongoose document

196 Views Asked by At

I'm using node-ipc to transfer some mongoose documents across child processes, however, Mongoose functions no longer work with these documents since stringify is called and they are no longer valid mongoose documents.

What is the best way to make this work? Is there a way to convert them back to Mongoose docs when they are received by child processes?

1

There are 1 best solutions below

0
On BEST ANSWER

Mongoose provides the .hydrate() function that allows you to create a mongoose document from plain json objects (that already exist in the DB). So you should be able to do:

const mongooseDoc = YourModel.hydrate(JSON.parse(yourStringifedObject));