Categories
JavaScript Answers

How to populate after save with Mongoose?

Spread the love

Sometimes, we want to populate after save with Mongoose.

In this article, we’ll look at how to populate after save with Mongoose.

How to populate after save with Mongoose?

To populate after save with Mongoose, we can use the populate and execPopulate method after calling save.

For instance, we write

const t = new MyModel(value)
await t.save()
t.populate('my-path').execPopulate()

to call t.save to save t in the database.

Then we call t.populate with the property to populate.

And then we call execPopulate to run the populate operation.

Conclusion

To populate after save with Mongoose, we can use the populate and execPopulate method after calling save.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *