mongoose的lean的用法

项目中遇到一个问题,我使用find方法查询出来的结果,想通过JSON.stringify()将其序列化,发现没法操作,然后在stackoverflow找到了一个答案:

http://stackoverflow.com/questions/9952649/convert-mongoose-docs-to-json

这里使用到了lean方法

官方解释:

Documents returned from queries with the lean option enabled are plain javascript objects,
not MongooseDocuments. They have no save method, getters/setters or other Mongoose magic applied.

使用该方法,查询出来的数据是一个javascript对象,不是一个mongoose文档,不再具有save,getters/setters和其它mongoose提供的方法。

Example:

new Query().lean() // true
new Query().lean(true)
new Query().lean(false)

Model.find().lean().exec(function (err, docs) {
  docs[0] instanceof mongoose.Document // false
});


http://mongoosejs.com/docs/api.html#query_Query-lean