Default Mongoose to use lean queries. Fatten them back up with syntactic sugar.
First a word of warning: the current implementation patches mongoose directly, which means if any of your code or dependencies expects the default un-patched mongoose, you'll be in trouble. I'm interested in changing the implementation to opt-in to this behavior on a per-model basis, which should avoid this.
npm install mongoose-lean
var mongoose = require('mongoose');
var patchMongoose = require('mongoose-lean');
patchMongoose(mongoose);
var User = mongoose.model('User', new mongoose.Schema({name: String}));
function weighIn(bob) {
console.log('bob is ', bob instanceof User ? 'fat' : 'lean');
}
User.create({name: 'Bob'}, function(err, bob) {
weighIn(bob); // => bob is fat
User.findById(bob._id, function(err, bob) {
weighIn(bob); // => bob is lean
User.findById(bob._id).fat().exec(function(err, bob) {
weighIn(bob); // => bob is fat
});
});
});
Please follow our Code of Conduct when contributing to this project.
$ git clone https://github.com/goodeggs/mongoose-lean && cd mongoose-lean
$ npm install
$ npm test
Module scaffold generated by generator-goodeggs-npm.