abc
abc

Reputation: 31

How can i convert the mongoose aggregation array of object of object response into json object response

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?

Upvotes: 0

Views: 751

Answers (1)

vizsatiz
vizsatiz

Reputation: 2183

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

Upvotes: 1

Related Questions