books_id field_id, title, and author fields_id fieldsbooks_id field_id, title, and author fields_id fieldsbooks collection with one bookbooks and insert one document with _id set to "book1", title set to "The Great Gatsby", and author set to "F. Scott Fitzgerald".Use db.books.insertOne() with a document that includes _id, title, and author.
_idsecondBookId and set it to the string "book2".Use const secondBookId = "book2" to store the custom ID for the next book.
secondBookId variablebooks collection with _id set to the variable secondBookId, title set to "1984", and author set to "George Orwell".Use db.books.insertOne() with _id: secondBookId and the other fields.
_id directlybooks collection with _id set to "book3", title set to "To Kill a Mockingbird", and author set to "Harper Lee".Use db.books.insertOne() with the specified fields and _id value.
What is the purpose of the _id field in a MongoDB document?
_id in MongoDB_id field is a unique identifier for each document in a collection, ensuring no two documents share the same _id._id = unique document ID [OK]_id means unique ID for each document [OK]_id stores creation date_id with user data fields_id is optionalWhich of the following is the correct way to insert a document with a custom _id value in MongoDB?
db.users.insertOne({ _id: 123, name: "Alice" })_id field usage_id field must be named exactly _id to set a custom ID. db.users.insertOne({ _id: 123, name: "Alice" }) uses _id: 123 correctly.id instead of _id. db.users.insertOne({ _id: "name", name: "Alice" }) uses a string "name" which is valid but less meaningful here. db.users.insertOne({ _id: ObjectId(), name: "Alice" }) uses default ObjectId, not custom._id needs exact field name [OK]_id for custom IDs [OK]id instead of _id_idGiven the following documents inserted into a collection:
<pre>db.products.insertMany([ { _id: "p1", name: "Pen" }, { _id: "p2", name: "Pencil" }, { _id: "p3", name: "Eraser" } ]) What willdb.products.find({ _id: "p2" }).toArray() return?_id equal to "p2"._id: "p2" has the name "Pencil" and exists in the collection._id returns matching document [OK]_id returns matching document [OK]_id with other fields_idConsider this insertion attempt:
db.orders.insertOne({ _id: 101, item: "Book" })
db.orders.insertOne({ _id: 101, item: "Notebook" })What error will occur and why?
_id_id field must be unique in a collection. Both documents use _id: 101._id_id causes a DuplicateKeyError._id must be unique. -> Option D_id causes insertion error [OK]_id [OK]_id can repeat_id must be string onlyYou want to store user profiles with custom _id values based on their email addresses to speed up lookups. Which approach is best?
// Option A
{ _id: ObjectId(), email: "user@example.com", name: "User" }
// Option B
{ _id: "user@example.com", name: "User" }
// Option C
{ email: "user@example.com", name: "User" }
// Option D
{ _id: UUID(), email: "user@example.com", name: "User" }_id_id to the email string enables direct lookup using the email as _id, making queries fast. Using default ObjectId or UUID with separate email requires additional indexing. Omitting _id is invalid since it is mandatory._id to the email string for direct lookup. -> Option C_id as email speeds queries [OK]_id for fast direct lookups [OK]_id uniqueness requirementPass the test and this pattern moves unknown