Convert Mongoose docs to json

Trantor Liu picture Trantor Liu · Mar 31, 2012 · Viewed 114.1k times · Source

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

ecdeveloper picture ecdeveloper · Jan 2, 2013

You may also try mongoosejs's lean() :

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