0
0
MongoDBquery~20 mins

insertMany method in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB InsertMany Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this insertMany operation?

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?

MongoDB
db.students.insertMany([{ name: "Alice", age: 20 }, { name: "Bob", age: 22 }])
A{"acknowledged": true, "insertedCount": 2, "insertedIds": {"0": ObjectId(...), "1": ObjectId(...)}}
B{"acknowledged": false, "insertedCount": 0, "insertedIds": {}}
CAn error because insertMany only accepts one document
D{"acknowledged": true, "insertedCount": 1, "insertedIds": {"0": ObjectId(...)}}
Attempts:
2 left
💡 Hint

insertMany inserts multiple documents and returns info about all inserted documents.

📝 Syntax
intermediate
1:30remaining
Which option is a correct syntax for insertMany?

Which of the following is the correct syntax to insert multiple documents into a MongoDB collection orders using insertMany?

Adb.orders.insertMany({ item: "book", qty: 5 }, { item: "pen", qty: 10 })
Bdb.orders.insertMany([{ item: "book", qty: 5 }, { item: "pen", qty: 10 }])
Cdb.orders.insertMany([{ item: "book", qty: 5 }], [{ item: "pen", qty: 10 }])
Ddb.orders.insertMany(item: "book", qty: 5, item: "pen", qty: 10)
Attempts:
2 left
💡 Hint

insertMany expects a single array of documents as its argument.

optimization
advanced
1:30remaining
Why use insertMany instead of multiple insertOne calls?

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?

AinsertMany sends all documents in one request, reducing network overhead and improving performance.
BinsertMany validates documents more strictly than insertOne.
CinsertMany automatically creates indexes for the documents.
DinsertMany can only insert documents with the same fields.
Attempts:
2 left
💡 Hint

Think about how many times the database is contacted.

🔧 Debug
advanced
2:00remaining
What error occurs with this insertMany call?

Given the following code:

db.products.insertMany([
  { name: "Table", price: 100 },
  { name: "Chair" price: 50 }
])

What error will MongoDB raise?

ANo error, documents inserted successfully
BDuplicateKeyError because of repeated document fields
CTypeError because insertMany expects an object, not an array
DSyntaxError due to missing comma between fields in second document
Attempts:
2 left
💡 Hint

Look carefully at the second document's fields.

🧠 Conceptual
expert
1:30remaining
What happens if insertMany is called with an empty array?

What is the result of running db.collection.insertMany([]) in MongoDB?

AReturns null without any acknowledgment
BThrows an error because the array is empty
CReturns acknowledged true with insertedCount 0 and empty insertedIds
DInserts a single empty document into the collection
Attempts:
2 left
💡 Hint

Think about what happens when you insert zero documents.