Introduction
MongoDB automatically creates a unique _id for each document to identify it easily without extra work.
Jump into concepts and practice - no test required
db.collection.insertOne({ field1: value1, field2: value2 })db.users.insertOne({ name: "Alice", age: 30 })db.users.insertOne({ _id: "customID123", name: "Bob" })db.users.findOne({ _id: ObjectId("507f1f77bcf86cd799439011") })use testdb // Insert a document without _id var result = db.products.insertOne({ name: "Pen", price: 1.5 }) // Show the inserted document with auto-generated _id var doc = db.products.findOne({ _id: result.insertedId }) doc
_id field?_id_id. If not provided, it creates one automatically._id_id is an ObjectId, which is unique and generated by MongoDB._id for the document. -> Option A_id = unique ObjectId [OK]_id, MongoDB creates a unique one automatically [OK]_id_id can be null_id is a simple number_id in MongoDB shell?_id_id, so MongoDB generates it._id. db.collection.insertOne({_id: null, name: 'Alice'}) sets _id to null which is invalid. db.collection.insertOne({_id: 1, name: 'Alice'}) sets _id manually. db.collection.insertOne() is missing the document argument._id uses document only [OK]_id to auto-generate it [OK]_id to null explicitly_id assignmentdb.test.insertOne({name: 'Bob'})
db.test.insertOne({_id: ObjectId('507f1f77bcf86cd799439011'), name: 'Carol'})
db.test.find().count()
What will be the output of the count() command?_id, so MongoDB generates one. The second insert adds a document with a specific _id ObjectId._id in the second insert is unique and different from the first, both inserts succeed. So, the collection has 2 documents._id means both inserts succeed [OK]_id matches manual one_id error occursdb.users.insertOne({_id: 1, name: 'Dave'})
db.users.insertOne({_id: 1, name: 'Eve'})
What will happen and how can you fix it?_id uniqueness constraint_id to be unique in a collection. Duplicate _id values cause insert failure._id: 1 succeeds. The second insert tries the same _id, causing a duplicate key error._id; fix by using unique _id values. -> Option D_id causes insert failure [OK]_id must be unique to avoid insert errors [OK]_id_id must be ObjectId type_id without manually specifying it. Which approach correctly achieves this and why?
const docs = [
{name: 'Anna'},
{name: 'Ben'},
{name: 'Cara'}
];
db.collection.insertMany(docs);_id_id, MongoDB automatically creates a unique ObjectId for each during insert._id for each document. correctly relies on MongoDB's default behavior. Add _id: null to each document to let MongoDB generate _id. is invalid because _id: null is not allowed. Manually assign sequential integers as _id before insert. requires manual work and risks duplicates. Insert documents without _id but create a unique index on name. creates a unique index on name, unrelated to _id uniqueness._id for each document. -> Option B_id means MongoDB creates unique ObjectId [OK]_id to get unique ObjectId automatically [OK]_id to null explicitly_id unnecessarily_id