Consider a MongoDB collection named students. You run the following command:
db.students.insertMany([
{ name: "Alice", age: 20 },
{ name: "Bob", age: 22 }
])What does the insertMany method return?
db.students.insertMany([{ name: "Alice", age: 20 }, { name: "Bob", age: 22 }])insertMany inserts multiple documents and returns info about all inserted documents.
The insertMany method inserts all documents in the array and returns an object showing it succeeded (acknowledged: true), how many documents were inserted (insertedCount), and the IDs of inserted documents (insertedIds).
Which of the following is the correct syntax to insert multiple documents into a MongoDB collection orders using insertMany?
insertMany expects a single array of documents as its argument.
The insertMany method requires one array containing all documents to insert. Option B correctly passes an array of two documents.
You want to add 1000 new user documents to a MongoDB collection. Which is the best reason to use insertMany instead of calling insertOne 1000 times?
Think about how many times the database is contacted.
Using insertMany sends all documents in a single request, which reduces the number of network round-trips and speeds up the insertion process compared to calling insertOne repeatedly.
Given the following code:
db.products.insertMany([
{ name: "Table", price: 100 },
{ name: "Chair" price: 50 }
])What error will MongoDB raise?
Look carefully at the second document's fields.
The second document is missing a comma between "Chair" and price: 50, causing a syntax error in the JavaScript object.
What is the result of running db.collection.insertMany([]) in MongoDB?
Think about what happens when you insert zero documents.
Calling insertMany with an empty array returns an acknowledgment with zero documents inserted and an empty insertedIds object. It does not throw an error or insert anything.