0

I have you mongoose aggregation in nodejs to get the multiple distinct values of the documents. The response I got is an array of the object of objects. How can convert that into a general JSON object response?

1 Answer 1

1

You can use lean() function exposed by mongoose

And example would be:

YourModel.find().lean().exec(function (err, users) {
    return res.end(users);
}

OR

const leanDoc = await MyModel.findOne().lean();

Enabling the lean option tells Mongoose to skip instantiating a full Mongoose document and just give you the POJO.

Doc Link: https://mongoosejs.com/docs/tutorials/lean.html#using-lean

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.