Stop 05 · MongoDB FundamentalsProjects · 1/1

Library Catalogue in mongosh

Part of the MERN Stack Roadmap

In short

Reading about queries is not the same as writing them. In this project you build a small college library database from scratch in mongosh and answer the questions a real librarian would ask.

Build a small library database of books and members entirely in mongosh, practising inserts, filters, projections, sorting, updates and indexes on realistic data.

The jaadu, as a college librarian, stamps a book at a wooden desk while a student waits and tall shelves of books fill the room behind.

Think of it like this

Running the college library for a day

You are the new librarian. You stock the shelves (insert), find books students ask for (filter), show the newest arrivals (sort), record who borrowed what (update) and make the most-searched shelf easy to reach (index).

Watch it happen

Project milestones

Insert books
Insert members
Query
Borrow and return
Index

1/5Add about 20 books with title, author, genre, year, copies and tags.

The big ideas

What you need to know

Two collections

books and members. Loans can be embedded in members for this small project.

Question-driven

Write the librarian's questions first, then the query for each. That's how real API endpoints are designed.

Safe borrowing

Only decrement available copies when at least one is left, using a filter check plus $inc.

Save your script

Keep all commands in library.js and run it with mongosh --file so the project is repeatable and goes on GitHub.

See it in code

Walk through the code

js
1// library.js  (run: mongosh "$MONGODB_URI" --file library.js)2db = db.getSiblingDB("library");3db.books.drop();4 5db.books.insertMany([6  { title: "Wings of Fire", author: "A. P. J. Abdul Kalam", genre: "biography", year: 1999, available: 3 },7  { title: "The Guide", author: "R. K. Narayan", genre: "fiction", year: 1958, available: 2 },8  { title: "Clean Code", author: "Robert C. Martin", genre: "tech", year: 2008, available: 1 },9]);10 11// Fiction published before 2000, newest first, title only12printjson(db.books.find({ genre: "fiction", year: { $lt: 2000 } }, { title: 1, _id: 0 }).sort({ year: -1 }).toArray());13 14// Borrow a copy only if one is available15db.books.updateOne({ title: "Clean Code", available: { $gt: 0 } }, { $inc: { available: -1 } });16db.members.updateOne({ memberId: "M101" }, { $push: { loans: { title: "Clean Code", on: new Date() } } }, { upsert: true });17 18db.books.createIndex({ genre: 1, year: -1 });

1/5Switch to the library database and start fresh each run.

The starting script. Extend it with more books, members and at least eight librarian questions.

Save yourself the pain

Common mistakes

Oops

Typing everything into the shell and losing it when the terminal closes.

Do this instead

Keep the commands in a .js file and run it with --file. Commit it to GitHub.

Oops

Allowing available copies to go negative.

Do this instead

Put available: { $gt: 0 } in the update filter so the decrement only happens when a copy exists.

Your mission

Finish the catalogue

~60 min

Quick check

Which update safely borrows a book only if a copy is available?

Go deeper

  1. 1Insert at least 30 books with authors, genres, copies available and publish years.
  2. 2Write queries such as all available Hindi novels published after 2010, sorted by title.
  3. 3Save your queries in a .js file in a GitHub repository with a README.

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge