0
0
Expressframework~10 mins

Finding and querying documents in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Finding and querying documents
Start Express app
Receive HTTP GET request
Parse query parameters
Build query object
Call database find method
Database returns matching documents
Send documents as HTTP response
End request
The Express app receives a GET request, extracts query parameters, builds a query, fetches matching documents from the database, and sends them back as a response.
Execution Sample
Express
app.get('/users', async (req, res) => {
  const { age } = req.query;
  const users = await User.find({ age: Number(age) });
  res.json(users);
});
This code listens for GET requests on '/users', queries users by age, and returns matching users as JSON.
Execution Table
StepActionInput/ConditionDatabase QueryResultResponse Sent
1Receive GET requestGET /users?age=30N/AN/AN/A
2Parse query parametersage=30N/AN/AN/A
3Build query object{ age: 30 }N/AN/AN/A
4Call User.find(){ age: 30 }Find users with age 30[{name: 'Alice', age: 30}, {name: 'Bob', age: 30}]N/A
5Send responseN/AN/AN/AJSON array of users with age 30
6End requestN/AN/AN/AResponse complete
💡 Request ends after sending JSON response with matching documents.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
req.query.ageundefined'30''30''30''30'
queryObject{}{}{ age: 30 }{ age: 30 }{ age: 30 }
usersundefinedundefinedundefined[{name: 'Alice', age: 30}, {name: 'Bob', age: 30}][{name: 'Alice', age: 30}, {name: 'Bob', age: 30}]
Key Moments - 2 Insights
Why is the age value a string ('30') in the query object instead of a number?
Query parameters from the URL are always strings. To query by number, you must convert the string to a number before building the query object. See Step 3 in execution_table where age is still a string.
What happens if no documents match the query?
The database returns an empty array. The response will send an empty JSON array. This is shown in Step 4 where the result could be [].
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what does the database return?
AA single user object
BAn empty object
CAn array of user objects with age 30
DAn error message
💡 Hint
Check the 'Result' column in Step 4 of execution_table
At which step is the query object created?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table where the query object is built
If the URL query parameter was age=25, how would the 'users' variable change after Step 4?
AIt would contain users with age 25
BIt would contain users with age 30
CIt would be undefined
DIt would contain all users
💡 Hint
Refer to variable_tracker and execution_table Step 4 for how query affects users
Concept Snapshot
Express GET route listens for requests
Extract query parameters from req.query
Build a query object for database
Use Model.find(query) to get matching documents
Send results as JSON response
Query params are strings; convert if needed
Full Transcript
This example shows how an Express app handles finding and querying documents. When a GET request arrives at '/users' with a query parameter like age=30, the app extracts the age from req.query. It builds a query object with this age and calls User.find() to get matching users from the database. The database returns an array of user objects matching the age. Finally, the app sends this array as a JSON response. Note that query parameters are strings, so if you want to query by number, you should convert the string to a number before querying. If no users match, an empty array is returned and sent. This flow helps you understand how to find and query documents in Express apps.