When should I use MongoDB transactions?
For multi-document operations that must be atomic. If two updates must succeed together or fail together, use a transaction. MongoDB 4.0+ supports transactions, and Node.js Mongoose supports them.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Common MongoDB Mistakes in Node.js That Cause Performance Issues
Not adding indexes on frequently queried fields. Without indexes, every query scans the entire collection, which is slow as data grows. Add indexes for fields you query often, like email and userIds.
Fetching a list of items, then querying for each item's related data separately, causing N+1 queries instead of one. Use Mongoose populate to join related data in one query, avoiding the N+1 problem.
Use projections to fetch only the fields you need. Instead of User.find() to get entire documents when you only need the name, use User.find({}, { name: 1, _id: 0 }) to get just the name field.
Still have questions?
Browse all our FAQs or reach out to our support team
