Interview Revision
Revision: Fundamentals and CRUD
MongoDB vs SQL, BSON, ObjectId, and the write/read/update/delete surface.
BeginnerAbout 6 minutes
- Document DB. Database → collection → document → field. Rahul is one document in
users. - vs SQL. Embed or reference; no JOIN as the default read. Schema in documents (validators optional). Single-document atomicity first.
- BSON. Typed binary JSON: ObjectId, Date, Decimal128, int vs double. Length-prefixed. Money is not a Double.
- `_id`. Unique per collection. Default ObjectId: time + randomness. Do not invent string ids unless you have a reason.
_idis already indexed.
db.users.insertOne({ name: "Rahul", city: "Bangalore" })
db.users.find({ city: "Bangalore" }, { name: 1, _id: 0 })
db.users.findOne({ email: "rahul@example.com" }) // document or null
db.users.updateOne({ email }, { $set: { city: "Pune" }, $inc: { age: 1 } })
db.users.deleteOne({ email })
db.users.updateOne({ email }, { $set: { city: "Pune" } }, { upsert: true })- find = cursor; findOne = one doc or null.
- updateOne + operators, not replace, unless you mean
replaceOne. - matchedCount vs modifiedCount. Filter on
updateOnelike a DELETE. - `$inc` on the server — not find, subtract,
$set.
Interview question
How is BSON different from JSON?
Think about it first.
BSON is a binary, typed encoding — ObjectId, Date, Decimal128, distinct integers — and length-prefixed so fields can be skipped. JSON has none of that. Use Decimal128 or integer cents for money.
Interview question
When do you use MongoDB instead of SQL?
Think about it first.
When the common read is one document with nested data, the shape is still moving, and I do not want a join on the hot path. SQL when the workload is relational reports and multi-row writes as the default.
Practice