QA
Intermediate LevelMongoDB & Mongoose

What are MongoDB Read Concerns (local, majority, linearizable)?

❓ Interview Prompt: Explain Read Concerns (local, available, majority, linearizable, snapshot) and controlling dirty read visibility.

Read Concern controls the durability and consistency level of data read from MongoDB:

•
'local' (Default): Returns node's most recent data; risk of reading data that might be rolled back if primary crashes.
•
'majority': Returns data acknowledged by a majority of replica nodes (prevents dirty reads).
•
'linearizable': Guarantees real-time primary node consistency.

Code Implementation Example

typescript
import { AccountModel } from './models/Account';

async function getGuaranteedBalance(accountId: string) {
  // Ensures read data cannot be rolled back!
  return await AccountModel.findById(accountId)
    .readConcern('majority')
    .exec();
}
💡Executing majority read concern to prevent reading rolled-back data.
🎯 Interview Key Takeaway

Use majority readConcern for financial reads to ensure queried data cannot be rolled back during failover.

#Read Concern#majority#Dirty Reads#Intermediate
Back to all MongoDB & Mongoose Questions→

More MongoDB & Mongoose Interview Questions

Beginner LevelMongoDB & Mongoose

How does the MongoDB Aggregation Pipeline work? Explain $match, $group, $lookup, and $project.

Beginner LevelMongoDB & Mongoose

What is MongoDB and how does the BSON document data model work?

Beginner LevelMongoDB & Mongoose

What is the structure of a MongoDB ObjectId and how is it generated?