Categories
JavaScript Answers

How to convert Mongoose docs to JSON?

Spread the love

Sometimes, we want to convert Mongoose docs to JSON

In this article, we’ll look at how to convert Mongoose docs to JSON.

How to convert Mongoose docs to JSON?

To convert Mongoose docs to JSON, we can use the lean method.

For instance, we write

UserModel.find().lean().exec((err, users) => {
  console.log(users);
})

to call lean after find to convert the query results returned by find to a plain JavaScript object.

Then we call exec with a callback to get the returned query results from users.

Conclusion

To convert Mongoose docs to JSON, we can use the lean method.

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 *