lean()

noun

say it: “LEEN” /ˈlin/

A Mongoose query option that returns plain JavaScript objects instead of full Mongoose documents.

What does lean() do in Mongoose?

lean() tells a Mongoose query to give back plain JavaScript objects instead of full Mongoose documents. They're faster and lighter, but have no save() or other document methods, so it's for code that only reads.

const cats = await Pet.find({ species: "cat" }).lean();
console.log(typeof cats[0].save);   // 'undefined': a plain object

In a sentence

  • “The listing route reads pets with lean(), since it only sends them on as JSON.”
  • “After lean(), calling save() on the result failed, because it's a plain object.”

Level: Journeyman · More MongoDB terms at this level

Updated