Search code examples
node.jsmongodbmongoosenode-ipc

Convert stringified object to Mongoose document


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?


Solution

  • 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));