0
0
Expressframework~10 mins

Finding and querying documents in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find all documents in a MongoDB collection using Express.

Express
app.get('/items', async (req, res) => {
  const items = await Item.[1]();
  res.json(items);
});
Drag options to blanks, or click blank then click option'
Afind
Binsert
Cupdate
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert instead of find
Using update or delete which modify data
2fill in blank
medium

Complete the code to find documents matching a condition in Express.

Express
app.get('/users', async (req, res) => {
  const users = await User.find({ age: { [1]: 18 } });
  res.json(users);
});
Drag options to blanks, or click blank then click option'
A$lt
B$ne
C$gte
D$in
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt which means less than
Using $ne which means not equal
3fill in blank
hard

Fix the error in the code to find a single document by ID.

Express
app.get('/post/:id', async (req, res) => {
  const post = await Post.[1](req.params.id);
  res.json(post);
});
Drag options to blanks, or click blank then click option'
AfindOneById
BfindAllById
CfindByID
DfindById
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like findOneById or findByID
Using findAllById which does not exist
4fill in blank
hard

Fill both blanks to find documents where status is 'active' and sort by createdAt descending.

Express
const activeItems = await Item.find({ status: [1] }).[2]({ createdAt: -1 });
Drag options to blanks, or click blank then click option'
A'active'
Bsort
Cfilter
D'inactive'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'inactive' instead of 'active'
Using 'filter' instead of 'sort' for ordering
5fill in blank
hard

Fill all three blanks to find users older than 21, select only name and email fields, and limit results to 5.

Express
const users = await User.find({ age: { [1]: 21 } })
  .[2]('name email')
  .[3](5);
Drag options to blanks, or click blank then click option'
A$gt
Bselect
Climit
Dskip
Attempts:
3 left
💡 Hint
Common Mistakes
Using $gte instead of $gt
Using skip instead of limit
Not selecting fields properly