0
0
MongoDBquery~20 mins

Insert with arrays in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What documents are inserted with this array insert?
Given the following MongoDB insert command, what documents will be stored in the collection?
MongoDB
db.students.insertMany([
  { name: "Alice", scores: [85, 90, 88] },
  { name: "Bob", scores: [78, 82, 80] }
])
A[{ name: "Alice", scores: [85, 90, 88] }, { name: "Bob", scores: [78, 82, 80] }]
B[{ name: "Alice" }, { name: "Bob" }, { scores: [85, 90, 88] }, { scores: [78, 82, 80] }]
C[{ name: "Alice", scores: 85 }, { name: "Bob", scores: 78 }]
D[{ name: "Alice", scores: [85] }, { name: "Bob", scores: [78] }]
Attempts:
2 left
💡 Hint
Remember that insertMany inserts each object in the array as a separate document.
🧠 Conceptual
intermediate
1:30remaining
How does MongoDB handle arrays in inserted documents?
When inserting a document with an array field in MongoDB, how is the array stored?
AMongoDB does not support arrays in documents.
BEach element of the array is stored as a separate document.
CThe array elements are flattened into separate fields with numeric keys.
DThe array is stored as a single field containing all elements in order.
Attempts:
2 left
💡 Hint
Think about how JSON arrays are represented in MongoDB documents.
📝 Syntax
advanced
2:30remaining
Which insert command correctly inserts multiple documents with array fields?
Choose the correct MongoDB command to insert two documents, each having a 'tags' array field.
Adb.collection.insert([{ name: "Item1", tags: "red", "large" }, { name: "Item2", tags: "blue", "small" }])
Bdb.collection.insertMany({ name: "Item1", tags: ["red", "large"] }, { name: "Item2", tags: ["blue", "small"] })
Cdb.collection.insertMany([{ name: "Item1", tags: ["red", "large"] }, { name: "Item2", tags: ["blue", "small"] }])
Ddb.collection.insertMany([{ name: "Item1", tags: "red", "large" }, { name: "Item2", tags: "blue", "small" }])
Attempts:
2 left
💡 Hint
Check the syntax for insertMany and how arrays are represented.
optimization
advanced
3:00remaining
Best practice for inserting many documents with large arrays?
You need to insert 10,000 documents, each with a large array field. What is the best approach to optimize insertion speed in MongoDB?
AUse insertMany with batches of documents, each batch containing multiple documents with arrays.
BInsert each document one by one using insertOne to avoid memory issues.
CConvert arrays to strings before inserting to reduce size.
DInsert all 10,000 documents in a single insertMany call without batching.
Attempts:
2 left
💡 Hint
Think about balancing batch size and memory usage.
🔧 Debug
expert
3:00remaining
Why does this insertMany command fail?
Consider this command: db.products.insertMany([ { name: "Pen", colors: "blue", "red" }, { name: "Pencil", colors: ["yellow", "black"] } ]) Why does it fail?
AMongoDB does not allow arrays in documents.
BThe first document's 'colors' field is incorrectly formatted; array elements must be inside square brackets.
CinsertMany requires documents to have the same fields.
DThe second document's array syntax is invalid.
Attempts:
2 left
💡 Hint
Look carefully at how the 'colors' field is written in the first document.