Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Finding and Querying Documents with Express and MongoDB
📖 Scenario: You are building a simple Express server that connects to a MongoDB database. The database stores a collection of books with their titles and authors.You want to create routes to find and query these book documents.
🎯 Goal: Build an Express server with a MongoDB connection that can find all books and query books by author.
📋 What You'll Learn
Create a MongoDB collection named books with sample data
Set up an Express server with MongoDB connection
Create a route to find all books
Create a route to query books by author using a query parameter
💡 Why This Matters
🌍 Real World
Web servers often need to find and filter data from databases to show users relevant information.
💼 Career
Backend developers use Express and databases like MongoDB to build APIs that find and query documents efficiently.
Progress0 / 4 steps
1
Set up the books collection with sample data
Create a constant called books that is an array of objects with these exact entries: { title: 'The Hobbit', author: 'J.R.R. Tolkien' }, { title: '1984', author: 'George Orwell' }, and { title: 'To Kill a Mockingbird', author: 'Harper Lee' }.
Express
Hint
Use const books = [ ... ] with the exact objects inside the array.
2
Set up Express server and MongoDB connection
Create a constant called express by requiring 'express'. Then create a constant called app by calling express(). Also create a constant called port and set it to 3000.
Express
Hint
Use const express = require('express'), then const app = express(), and set const port = 3000.
3
Create route to find all books
Use app.get with the path '/books' and a callback with parameters req and res. Inside the callback, send the books array as JSON using res.json(books).
Express
Hint
Use app.get('/books', (req, res) => { res.json(books); }) to send all books.
4
Create route to query books by author
Use app.get with the path '/books/search' and a callback with req and res. Inside, get the author query parameter from req.query.author. Filter the books array to only include books where book.author exactly matches the author query. Send the filtered array as JSON with res.json(filteredBooks). Finally, start the server listening on port with app.listen(port).
Express
Hint
Use app.get('/books/search', (req, res) => { ... }), get author from req.query.author, filter books by exact author match, send filtered books with res.json, and start server with app.listen(port).
Practice
(1/5)
1. In Express with Mongoose, which method is used to find all documents matching a condition in a collection?
easy
A. Model.delete()
B. Model.create()
C. Model.update()
D. Model.find()
Solution
Step 1: Understand the purpose of Model.find()
This method retrieves documents from the database that match the given query conditions.
Step 2: Differentiate from other methods
Model.create() adds new documents, Model.update() modifies existing ones, and Model.delete() removes documents.
Final Answer:
Model.find() -> Option D
Quick Check:
Find documents = Model.find() [OK]
Hint: Use find() to get matching documents [OK]
Common Mistakes:
Confusing find() with create()
Using update() to retrieve documents
Trying to delete documents to find them
2. Which of the following is the correct syntax to find all users with age 30 using Mongoose in Express?
easy
A. User.find({ age: 30 })
B. User.find(age == 30)
C. User.find('age: 30')
D. User.find({ 'age' = 30 })
Solution
Step 1: Use an object to specify query conditions
The query must be an object with key-value pairs, like { age: 30 }.
Step 2: Check syntax correctness
User.find({ age: 30 }) uses correct JavaScript object syntax. Options B, C, and D have syntax errors or wrong formats.
Final Answer:
User.find({ age: 30 }) -> Option A
Quick Check:
Query object syntax = User.find({ age: 30 }) [OK]
Hint: Use curly braces with key: value for queries [OK]
Common Mistakes:
Using comparison operators inside find()
Passing query as a string
Using assignment (=) instead of colon (:) in object
3. What will be the output of this code snippet in Express using Mongoose?
C. The projection object is missing quotes around field names
D. The filter object should be a string
Solution
Step 1: Check the projection argument
The projection should be an object with field names as strings or keys, like { name: 1, price: 1 }.
Step 2: Identify the syntax error
Using { name, price } without values or quotes is invalid JavaScript object syntax here.
Final Answer:
The projection object is missing quotes around field names -> Option C
Quick Check:
Projection keys must be strings or key-value pairs [OK]
Hint: Projection keys need explicit values or quotes [OK]
Common Mistakes:
Using shorthand object keys without values in projection
Misunderstanding $gt operator
Thinking async/await is invalid with find()
5. You want to find all orders where the status is 'shipped' and the total is greater than 50, but only return the order ID and total fields. Which is the correct Mongoose query in Express?