Skip to content

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. _id is 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 })
CRUD surface — say these out loud
  • find = cursor; findOne = one doc or null.
  • updateOne + operators, not replace, unless you mean replaceOne.
  • matchedCount vs modifiedCount. Filter on updateOne like a DELETE.
  • `$inc` on the server — not find, subtract, $set.

Interview question

How is BSON different from JSON?

Think about it first.

Interview question

When do you use MongoDB instead of SQL?

Think about it first.

Practice