Performance: Finding and querying documents
This affects server response time and how quickly the client receives data after a query.
Jump into concepts and practice - no test required
app.get('/users', async (req, res) => { const users = await User.find({ active: true }).limit(50); res.json(users); });
app.get('/users', async (req, res) => { const users = await User.find(); res.json(users); });
| Pattern | Server Processing | Network Payload | Response Time | Verdict |
|---|---|---|---|---|
| Query all documents without filters | High (full collection scan) | Large (all data sent) | Slow (blocks response) | [X] Bad |
| Query with filters and limit | Low (uses indexes) | Small (limited data) | Fast (quick response) | [OK] Good |
Model.find()Model.create() adds new documents, Model.update() modifies existing ones, and Model.delete() removes documents.Model.find() -> Option DModel.find() [OK]find() to get matching documents [OK]{ age: 30 }.User.find({ age: 30 }) uses correct JavaScript object syntax. Options B, C, and D have syntax errors or wrong formats.User.find({ age: 30 }) -> Option AUser.find({ age: 30 }) [OK]const users = await User.find({ active: true }, 'name email');
console.log(users);{ active: true }, so only active users are returned.'name email' selects only these fields to be included in the returned documents.const results = await Product.find({ price: { $gt: 100 } }, { name, price });{ name: 1, price: 1 }.{ name, price } without values or quotes is invalid JavaScript object syntax here.{ status: 'shipped', total: { $gt: 50 } } to find orders with status 'shipped' and total greater than 50.'_id total' is valid for projection in Mongoose.Order.find({ status: 'shipped', total: { $gt: 50 } }, { _id: 1, total: 1 }) uses an object projection with 1s which is valid but the question asks for the best correct query. Order.find({ status: 'shipped', total: { $gt: 50 } }, '_id total') uses string projection which is simpler and correct. Order.find({ status: 'shipped' && total > 50 }, '_id total') has invalid filter syntax. Order.find({ status: 'shipped', total: { $gt: 50 } }, { _id: true, total: true }) uses boolean true instead of 1 which is invalid in Mongoose projection.Order.find({ status: 'shipped', total: { $gt: 50 } }, '_id total') -> Option BOrder.find({ status: 'shipped', total: { $gt: 50 } }, '_id total') [OK]