mongoose .find() method returns object with unwanted properties

Alternatively to Kevin B’s answer, you can pass {lean: true} as an option:

myModel.find({name: 'John'}, '-name', {lean: true}, function(err, results){
  log(results[0])
}

In MongoDB, the documents are saved simply as objects. When Mongoose retrieves them, it casts them into Mongoose documents. In doing so it adds all those keys that are being included in your for loop. This is what allows you to use all the document methods. If you won’t be using any of these, lean is a great option as it skips that entire process, increasing query speed. Potentially 3x as fast.

Leave a Comment