Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Insert Documents with Arrays in MongoDB
📖 Scenario: You are managing a small library database. Each book can have multiple authors. You want to store books with their authors as an array inside each document.
🎯 Goal: Create a MongoDB collection called books and insert documents where each book has a title and an array of authors.
📋 What You'll Learn
Create a books collection with at least two documents
Each document must have a title field with a string value
Each document must have an authors field which is an array of strings
Use the insertMany() method to add the documents
💡 Why This Matters
🌍 Real World
Storing books with multiple authors is common in library databases, publishing systems, and online bookstores.
💼 Career
Knowing how to insert documents with arrays is essential for working with MongoDB in real-world applications like content management and cataloging.
Progress0 / 4 steps
1
Create the books collection with two book documents
Create a variable called books and assign it an array with two objects. The first object should have title set to "The Great Gatsby" and authors set to an array with "F. Scott Fitzgerald". The second object should have title set to "Good Omens" and authors set to an array with "Neil Gaiman" and "Terry Pratchett".
MongoDB
Hint
Remember to use square brackets [] for arrays and curly braces {} for objects.
2
Prepare the books collection for insertion
Create a variable called collection and assign it the MongoDB collection named books using db.collection('books').
MongoDB
Hint
Use db.collection('books') to get the collection object.
3
Insert the books array into the collection
Use the insertMany() method on collection to insert the books array.
MongoDB
Hint
Call insertMany() on the collection and pass the books array.
4
Confirm the insertion by querying all documents
Create a variable called allBooks and assign it the result of collection.find().toArray() to get all documents from the books collection.
MongoDB
Hint
Use find() with no arguments to get all documents, then convert to array with toArray().
Practice
(1/5)
1. What does the insertMany() method do in MongoDB?
easy
A. Inserts multiple documents into a collection at once
B. Deletes multiple documents from a collection
C. Updates multiple documents in a collection
D. Finds multiple documents in a collection
Solution
Step 1: Understand the purpose of insertMany()
The insertMany() method is used to add several documents to a MongoDB collection in one operation.
Step 2: Compare with other operations
Deleting, updating, or finding documents are done by other methods like deleteMany(), updateMany(), and find().
Final Answer:
Inserts multiple documents into a collection at once -> Option A
Quick Check:
insertMany() = Inserts multiple documents [OK]
Hint: insertMany() adds many documents in one call [OK]
Common Mistakes:
Confusing insertMany() with update or delete methods
Thinking insertMany() inserts only one document
Assuming insertMany() returns documents instead of inserting
2. Which of the following is the correct syntax to insert multiple documents using insertMany()?
easy
A. db.collection.insertMany('name: Alice', 'name: Bob')
B. db.collection.insertMany({name: 'Alice'}, {name: 'Bob'})
C. db.collection.insertMany([{name: 'Alice'}, {name: 'Bob'}])
D. db.collection.insertMany(name: 'Alice', name: 'Bob')
Solution
Step 1: Check the parameter type for insertMany()
The method requires an array of documents, so the argument must be inside square brackets [].
Step 2: Validate the options
Only db.collection.insertMany([{name: 'Alice'}, {name: 'Bob'}]) correctly passes an array of objects. The other options pass multiple arguments or wrong types, causing syntax errors.
Final Answer:
db.collection.insertMany([{name: 'Alice'}, {name: 'Bob'}]) -> Option C
Quick Check:
insertMany() needs an array of documents [OK]
Hint: Use square brackets [] to pass multiple documents [OK]
A. insertMany() cannot insert more than one document
B. Missing array brackets around documents
C. Documents have invalid field names
D. Using insertOne() instead of insertMany()
Solution
Step 1: Check the argument format for insertMany()
The method requires a single array containing all documents, but here documents are passed as separate arguments.
Step 2: Identify the fix
Wrapping the documents inside square brackets [] fixes the syntax: insertMany([{...}, {...}]).
Final Answer:
Missing array brackets around documents -> Option B
Quick Check:
insertMany() needs an array of documents [OK]
Hint: Always wrap multiple docs in [] for insertMany() [OK]
Common Mistakes:
Passing multiple documents as separate arguments
Confusing insertMany() with insertOne()
Assuming insertMany() accepts objects directly without array
5. You want to insert a list of users but only those with an age above 18. Which MongoDB operation correctly inserts only the valid users?
const users = [
{name: 'Anna', age: 17},
{name: 'Ben', age: 20},
{name: 'Cara', age: 22}
];
// Which code inserts only users older than 18?
hard
A. db.users.insertMany(users.filter(u => u.age > 18))
B. db.users.insertMany(users)
C. db.users.insertMany(users.map(u => u.age > 18))
D. db.users.insertMany(users.filter(u => u.age < 18))
Solution
Step 1: Filter users by age before insertion
Use JavaScript's filter() to keep only users with age greater than 18.
Step 2: Pass filtered array to insertMany()
Passing the filtered array inserts only valid users. Inserting all users includes invalid ones, mapping to booleans creates invalid documents, and filtering under 18 selects the wrong users.
Final Answer:
db.users.insertMany(users.filter(u => u.age > 18)) -> Option A
Quick Check:
Filter first, then insertMany() [OK]
Hint: Filter array before insertMany() to insert selected docs [OK]