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.