Trantor Liu Trantor Liu - 2 months ago 33
JSON Question

Convert Mongoose docs to json

I returned mongoose docs as json in this way:

UserModel.find({}, function (err, users) {
return res.end(JSON.stringify(users));
}


However, user.__proto__ was also returned. How can I return without it? I tried this but not worked:

UserModel.find({}, function (err, users) {
return res.end(users.toJSON()); // has no method 'toJSON'
}

Answer

You may also try mongoosejs's lean() :

UserModel.find().lean().exec(function (err, users) {
    return res.end(JSON.stringify(users));
}
Comments