Performance: Updating documents
MEDIUM IMPACT
This affects server response time and client perceived latency when updating data that triggers UI changes.
app.put('/item/:id', async (req, res) => { const id = req.params.id; const update = req.body; const result = await db.collection('items').findOneAndUpdate( {_id: new ObjectId(id)}, {$set: update}, {returnDocument: 'after'} ); res.send(result.value); });
app.put('/item/:id', async (req, res) => { const id = req.params.id; const update = req.body; const doc = await db.collection('items').findOne({_id: new ObjectId(id)}); Object.assign(doc, update); await db.collection('items').updateOne({_id: new ObjectId(id)}, {$set: doc}); res.send(doc); });
| Pattern | DB Calls | Server Delay | Network Payload | Verdict |
|---|---|---|---|---|
| Fetch then update | 2 calls (findOne + updateOne) | High (extra 50-100ms) | Larger (full doc sent back) | [X] Bad |
| Atomic findOneAndUpdate | 1 call | Low (minimal delay) | Smaller (only updated doc) | [OK] Good |