Details, details
findById() vs. findOne()
Section titled “findById() vs. findOne()”Mongoose provides specialized methods for single documents:
findById(id): Searches specifically for the_idfield.findOne({ field: value }): Searches for the first document matching a custom criteria.
When to use findById()
Section titled “When to use findById()”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.
Why Slugs?
Section titled “Why Slugs?”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.
findOne() in action
Section titled “findOne() in action”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 });}Slugs in the Router
Section titled “Slugs in the Router”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"); }});The Detail View
Section titled “The Detail View”views/project-detail.ejs
<article> <h2><%= project.title %></h2> <p><%= project.description %></p> <a href="/projects">← Back to Gallery</a></article>⏭ Next: Narrowing the Search
Section titled “⏭ Next: Narrowing the Search”We can find all. We can find one. Now let’s find some.