Skip to content

Details, details

Mongoose provides specialized methods for single documents:

  1. findById(id): Searches specifically for the _id field.
  2. findOne({ field: value }): Searches for the first document matching a custom criteria.

findById() is the workhorse of internal operations. It’s guaranteed to be unique and is the fastest way to retrieve a specific document.

Perfect for:

  • Admin Dashboards: Editing a specific record (/admin/projects/65c123.../edit).
  • Relationships: Looking up a user by their ID stored in a session.
  • API Endpoints: Where the client already knows the ID.

However, for public-facing pages, IDs are clunky. That’s where findOne() shines.

IDs like 65c123... are ugly. URLs like /projects/my-cool-app (using slugs) are better for:

  • SEO: Search engines can read the keywords.
  • UX: Users can read the URL.

Watch out!! Slugs are fragile. Ensure they strictly contain lowercase letters, numbers, and hyphens. A space, a capital letter, or a special character (like ?, &, or /) can break our URL logic or even crash the router.

data/projects.js

async getProjectBySlug(slug) {
// We use findOne because we are searching by a custom field (slug)
return await Project.findOne({ slug: slug, isActive: true });
}

routers/projectRouter.js

We use a route parameter (:slug) to capture the project identifier from the URL.

router.get("/:slug", async (req, res) => {
const { slug } = req.params;
const project = await _projectOps.getProjectBySlug(slug);
if (project) {
res.render("project-detail", { project });
} else {
res.status(404).render("404");
}
});

views/project-detail.ejs

<article>
<h2><%= project.title %></h2>
<p><%= project.description %></p>
<a href="/projects">← Back to Gallery</a>
</article>

We can find all. We can find one. Now let’s find some.