Search⌘ K
AI Features

Reading Documents: find(), findOne()

Explore how to perform read operations in MongoDB using Mongoose within a MERN stack backend. Understand how to use find() to retrieve multiple documents and findOne() for single documents by business identifier. Learn techniques like projection to select fields, using .lean() for faster read-only queries, and returning appropriate 404 responses when documents are not found. This lesson helps you build efficient and production-ready read endpoints for your API.

Learn the read step in CRUD with Mongoose. Use find() to return multiple documents and findOne() to return a single matching document by an _id value received as a string route parameter. Use projections to select the fields returned, use .lean() when you want plain JavaScript objects instead of full Mongoose documents, and return 404 when a single-resource query finds no matching document.

Reading is the most frequent operation in any API. A product list page, a detail view, and a search filter, all of them are reads. In a MERN backend, a GET request reaches a controller, the controller calls a Mongoose query method, and the result is sent back as JSON. This lesson covers the two core read methods and the two techniques, projection and .lean(), that appear in almost every production read endpoint.

Recall the course convention: the business identifier is the string field id (for example "2001"), not MongoDB’s internal _id. Single-document reads therefore use findOne({ id }), never findById().

Note: The examples are runnable. Each opens a local connection to the seeded ecommerce database, ...