Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a single document into the collection.
MongoDB
db.collection.[1]({ name: "Alice", age: 25 })
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using insertMany instead of insertOne for a single document.
Using findOne which only reads data.
Using updateOne which modifies existing data.
✗ Incorrect
The insertOne method inserts a single document into the collection.
2fill in blank
mediumComplete the code to insert a document and handle the returned promise.
MongoDB
db.collection.insertOne({ item: "book", qty: 10 }).[1](result => console.log(result.insertedId)) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using catch instead of then for success handling.
Using await without async function.
Using finally which runs regardless of success or failure.
✗ Incorrect
The then method handles the promise when the insertion succeeds.
3fill in blank
hardFix the error in the code to correctly insert a document.
MongoDB
db.collection.insertOne({ name: "Bob", age: 30 [1] ) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Closing the object with a square bracket instead of curly brace.
Missing the closing brace causes syntax errors.
Adding a semicolon inside the method call.
✗ Incorrect
The document object must be closed with a curly brace } before the closing parenthesis.
4fill in blank
hardFill both blanks to insert a document and print the inserted ID.
MongoDB
db.collection.[1]({ name: "Eve", active: true }).[2](res => console.log(res.insertedId))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using insertMany for a single document.
Using catch instead of then for success handling.
✗ Incorrect
Use insertOne to add one document and then to handle the success.
5fill in blank
hardFill all three blanks to insert a document, handle success, and catch errors.
MongoDB
db.collection.[1]({ username: "john_doe" }).[2](res => console.log(res.insertedId)).[3](err => console.error(err))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using insertMany instead of insertOne.
Mixing up then and catch methods.
✗ Incorrect
Use insertOne to insert, then to handle success, and catch to handle errors.