MCQ for Day 7: MongoDB CRUD Operations – Part 3 (Update)

Rashmi Mishra
0

  MCQ for Day 7: MongoDB CRUD Operations – Part 3 (Update)

1. Which method is used to update a single document in MongoDB?

A) updateMany()
B) updateOne()
C) find()
D) insertOne()

Answer: B) updateOne()

Explanation: The updateOne() method is used to update a single document in MongoDB that matches a given filter.


2. Which MongoDB operator is used to increment a field’s value?

A) $push
B) $set
C) $inc
D) $addToSet

Answer: C) $inc

Explanation: The $inc operator is used to increment the value of a field by a specified amount.


3. What is the purpose of the $set operator in MongoDB update operations?

A) Set a field’s value
B) Remove a field
C) Replace a field’s value
D) Add a new array element

Answer: A) Set a field’s value

Explanation: The $set operator is used to set a field to a specific value. If the field doesn’t exist, it is created.


4. What does the updateMany() method do?

A) Updates a single document
B) Updates all documents in a collection
C) Updates all documents that match a filter
D) Replaces a document completely

Answer: C) Updates all documents that match a filter

Explanation: The updateMany() method updates all documents in a collection that match the given filter.


5. Which MongoDB update method will replace a document entirely?

A) updateMany()
B) replaceOne()
C) updateOne()
D) setOne()

Answer: B) replaceOne()

Explanation: The replaceOne() method replaces an entire document in MongoDB based on the given filter.


6. What is the result of using updateOne() without the upsert option when no document matches the filter?

A) It will insert a new document
B) It will do nothing
C) It will return an error
D) It will update all documents

Answer: B) It will do nothing

Explanation: If no document matches the filter and the upsert option is not set, updateOne() will do nothing.


7. What does the upsert option do in MongoDB?

A) It replaces the document even if it doesn’t exist
B) It inserts a new document if no matching document is found
C) It performs a rollback operation
D) It deletes documents that don’t match the filter

Answer: B) It inserts a new document if no matching document is found

Explanation: The upsert option allows MongoDB to insert a new document if no documents match the filter.


8. What is the correct syntax to use the $push operator to add an item to an array?

A) { $push: { field: "value" } }
B) { $set: { field: ["value"] } }
C) { $add: { field: "value" } }
D) { $push: { "field": ["value"] } }

Answer: A) { $push: { field: "value" } }

Explanation: The $push operator is used to append an item to an array field.


9. Which of the following operators is used to remove a specific value from an array in MongoDB?

A) $addToSet
B) $pull
C) $push
D) $unset

Answer: B) $pull

Explanation: The $pull operator is used to remove a specific value from an array field in a MongoDB document.


10. How can you rename a field in MongoDB?

A) $rename
B) $set
C) $update
D) $change

Answer: A) $rename

Explanation: The $rename operator is used to rename a field in a MongoDB document.


11. What will the following query do in MongoDB: db.collection.updateOne({ age: 25 }, { $inc: { age: 1 } })?

A) Increment the age of all documents by 1
B) Replace the document where age is 25 with new data
C) Increment the age by 1 for the first document where age is 25
D) Set the age to 1 for the document where age is 25

Answer: C) Increment the age by 1 for the first document where age is 25

Explanation: This query uses the $inc operator to increment the age field by 1 for the first document where age is 25.


12. Which method can be used to update multiple documents in MongoDB?

A) updateMany()
B) updateOne()
C) insertMany()
D) findOneAndUpdate()

Answer: A) updateMany()

Explanation: The updateMany() method is used to update multiple documents that match a filter.


13. How would you remove a field from a document using MongoDB?

A) $remove
B) $unset
C) $delete
D) $drop

Answer: B) $unset

Explanation: The $unset operator is used to remove a field from a document.


14. What is the output of this query: db.students.updateOne({ name: "Alice" }, { $set: { grade: "A+" } })?

A) All students will have their grade updated to "A+"
B) Only the student with the name "Alice" will have their grade updated to "A+"
C) "A+" will be added to an array field
D) "Alice" will be replaced with a new document

Answer: B) Only the student with the name "Alice" will have their grade updated to "A+"

Explanation: The query updates the grade of the first document where the name is "Alice" to "A+".


15. What will happen if you use the replaceOne() method in MongoDB?

A) The document is appended with new fields
B) The document is entirely replaced with new data
C) The document is deleted
D) A new document is created with the same data

Answer: B) The document is entirely replaced with new data

Explanation: The replaceOne() method replaces the entire document with new data, leaving no old data.


16. How do you update a document conditionally based on its current value?

A) Using $eq operator
B) Using $set operator
C) Using $inc operator
D) Using $cond operator

Answer: A) Using $eq operator

Explanation: The $eq operator allows you to perform conditional updates based on the current value of a field.


17. What does the $addToSet operator do?

A) Adds an item to a field
B) Adds an item to an array, but only if it’s not already present
C) Appends an item to a document
D) Adds multiple items to an array

Answer: B) Adds an item to an array, but only if it’s not already present

Explanation: The $addToSet operator adds an item to an array field only if that item is not already present, ensuring uniqueness.


18. What happens when the updateOne() method is executed with no matching documents and without upsert?

A) A new document is inserted
B) The operation returns an error
C) No change occurs
D) All documents are updated

Answer: C) No change occurs

Explanation: Without a match and without the upsert option, updateOne() does nothing.


19. Which of the following is a correct way to use the $set operator to update multiple fields in MongoDB?

A) { $set: { field1: "value1", field2: "value2" } }
B) { $push: { field1: "value1", field2: "value2" } }
C) { $set: { field1: "value1" }, { field2: "value2" } }
D) { $inc: { field1: "value1", field2: "value2" } }

Answer: A) { $set: { field1: "value1", field2: "value2" } }

Explanation: The $set operator can update multiple fields in a document by specifying them in an object.


20. How do you update a field in a MongoDB document using the $mul operator?

A) By multiplying the field's value by a specified number
B) By adding a value to the field
C) By removing a field
D) By replacing the field’s value completely

Answer: A) By multiplying the field's value by a specified number

Explanation: The $mul operator is used to multiply the value of a field by a specified number.


21. Which method should you use to find and update a document in a single operation?

A) findOneAndUpdate()
B) updateOne()
C) updateMany()
D) findOne()

Answer: A) findOneAndUpdate()

Explanation: The findOneAndUpdate() method finds a document and updates it in one atomic operation.


22. What does the $min operator do in a MongoDB update operation?

A) Sets a field to the minimum of its current value and a specified value
B) Removes the field with the lowest value
C) Inserts the minimum number of documents
D) Calculates the minimum value of an array

Answer: A) Sets a field to the minimum of its current value and a specified value

Explanation: The $min operator updates a field only if the specified value is less than the current value of the field.


23. What is the effect of the $max operator in MongoDB update operations?

A) Sets a field to the maximum value of its current value and a specified value
B) Deletes documents with the highest value
C) Appends the highest value to an array
D) Replaces a field with a maximum value from all documents

Answer: A) Sets a field to the maximum value of its current value and a specified value

Explanation: The $max operator updates a field only if the specified value is greater than the current value of the field.


24. How do you use the $rename operator in MongoDB to rename a field?

A) { $rename: { "oldField": "newField" } }
B) { $set: { "oldField": "newField" } }
C) { $rename: { "newField": "oldField" } }
D) { $replace: { "oldField": "newField" } }

Answer: A) { $rename: { "oldField": "newField" } }

Explanation: The $rename operator is used to rename a field by specifying the old and new field names.


25. Which method would you use to replace a document completely in MongoDB?

A) updateOne()
B) updateMany()
C) replaceOne()
D) findOneAndUpdate()

Answer: C) replaceOne()

Explanation: The replaceOne() method replaces the entire document with a new one in MongoDB.


(Continue creating more MCQs following a similar pattern to reach 100)

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are the remaining 75 MCQs related to MongoDB CRUD Operations – Part 3 (Update) with answers and explanations:


26. What happens when the $set operator is used on a non-existing field in MongoDB?

A) It returns an error
B) It creates the field with the specified value
C) It updates the existing field
D) It appends the value to the field

Answer: B) It creates the field with the specified value

Explanation: The $set operator will create the field if it doesn't exist and set it to the specified value.


27. Which of the following is used to add an element to an array if it does not already exist?

A) $push
B) $addToSet
C) $pushAll
D) $set

Answer: B) $addToSet

Explanation: The $addToSet operator ensures that an element is added to an array only if it does not already exist in the array.


28. How would you increment the value of the quantity field by 5 in MongoDB?

A) { $set: { quantity: quantity + 5 } }
B) { $inc: { quantity: 5 } }
C) { $increase: { quantity: 5 } }
D) { $increment: { quantity: 5 } }

Answer: B) { $inc: { quantity: 5 } }

Explanation: The $inc operator increments the value of a field by a specified number, in this case, 5.


29. Which of the following operators is used to perform a conditional update in MongoDB?

A) $eq
B) $if
C) $cond
D) $set

Answer: C) $cond

Explanation: The $cond operator allows for conditional updates in MongoDB, enabling different update values based on certain conditions.


30. When using the updateOne() method, what happens if the filter does not match any document?

A) The update is ignored
B) A new document is inserted
C) An error is thrown
D) All documents are updated

Answer: A) The update is ignored

Explanation: If no document matches the filter and upsert is not specified, the update will not be applied.


31. How would you replace a document entirely using an update operation in MongoDB?

A) Use replaceOne()
B) Use updateMany()
C) Use updateOne()
D) Use insertOne()

Answer: A) Use replaceOne()

Explanation: The replaceOne() method replaces a document completely in MongoDB.


32. Which operator can be used to add an item to an array only if it does not already exist in MongoDB?

A) $addToSet
B) $push
C) $pushAll
D) $set

Answer: A) $addToSet

Explanation: The $addToSet operator adds a value to an array only if it is not already present, ensuring uniqueness.


33. Which MongoDB method is used to update an existing document, but only one document?

A) update()
B) updateOne()
C) updateMany()
D) findOneAndUpdate()

Answer: B) updateOne()

Explanation: The updateOne() method is used to update a single document that matches the filter.


34. How would you add a value to an existing array in MongoDB?

A) $push
B) $set
C) $add
D) $append

Answer: A) $push

Explanation: The $push operator is used to append a value to an array field in MongoDB.


35. What is the purpose of the $unset operator in MongoDB?

A) Set a field to null
B) Remove a field from a document
C) Update the value of a field
D) Add a new field to a document

Answer: B) Remove a field from a document

Explanation: The $unset operator is used to remove a field from a document in MongoDB.


36. Which of the following is the correct way to replace a document in MongoDB?

A) { $replace: { field1: "value1" } }
B) { $set: { field1: "value1" } }
C) { replaceOne: { field1: "value1" } }
D) { replaceOne: { "_id": value1 } }

Answer: D) { replaceOne: { "_id": value1 } }

Explanation: replaceOne() is used to replace a document based on the filter, typically _id.


37. Which method would you use if you wanted to find and update a document atomically in MongoDB?

A) findOneAndReplace()
B) findOneAndUpdate()
C) updateOne()
D) updateMany()

Answer: B) findOneAndUpdate()

Explanation: The findOneAndUpdate() method finds a document and updates it in a single atomic operation.


38. What will happen if you set the upsert option to true in the updateOne() method?

A) The document will be inserted if no match is found
B) The document will be deleted if no match is found
C) The update will be ignored
D) It will return an error if no document matches

Answer: A) The document will be inserted if no match is found

Explanation: Setting upsert to true will insert a new document if no document matches the filter.


39. Which operator is used to add a value to an array without duplicates?

A) $push
B) $addToSet
C) $set
D) $increment

Answer: B) $addToSet

Explanation: The $addToSet operator adds a value to an array, ensuring there are no duplicates.


40. How do you increment a field by a specific value in MongoDB?

A) $inc
B) $push
C) $set
D) $addToSet

Answer: A) $inc

Explanation: The $inc operator is used to increment the value of a field by a specified amount.


41. What will happen if you update a document using the updateMany() method?

A) Only the first matching document will be updated
B) All documents matching the filter will be updated
C) A new document will be created for each matching document
D) The document will be replaced

Answer: B) All documents matching the filter will be updated

Explanation: The updateMany() method will update all documents in the collection that match the specified filter.


42. Which of the following operators would you use to append an element to an array in MongoDB?

A) $append
B) $addToSet
C) $push
D) $set

Answer: C) $push

Explanation: The $push operator appends an element to an array in MongoDB.


43. Which method can be used to delete a field from a document in MongoDB?

A) $delete
B) $unset
C) $remove
D) $drop

Answer: B) $unset

Explanation: The $unset operator removes a field from a MongoDB document.


44. What will happen if you update a non-existing document with the upsert option set to false?

A) The operation will fail
B) The operation will succeed but no document will be updated
C) A new document will be inserted
D) All documents will be updated

Answer: B) The operation will succeed but no document will be updated

Explanation: If the upsert option is false and no document matches the filter, no update will occur, but no error will be thrown.


45. What will the following query do in MongoDB: db.collection.updateOne({ name: "John" }, { $inc: { age: 1 } })?

A) Increments the age field by 1 for the document where name is "John"
B) Sets the age field to 1 for the document where name is "John"
C) Replaces the document where name is "John"
D) Appends "John" to an array

Answer: A) Increments the age field by 1 for the document where name is "John"

Explanation: The $inc operator increments the age field by 1 in the document where name is "John."


46. Which of the following is a correct example of using the $rename operator in MongoDB?

A) { $rename: { "oldField": "newField" } }
B) { $set: { "oldField": "newField" } }
C) { $replace: { "oldField": "newField" } }
D) { $move: { "oldField": "newField" } }

Answer: A) { $rename: { "oldField": "newField" } }

Explanation: The $rename operator allows renaming a field in a document.


47. Which method would you use to update multiple documents in MongoDB?

A) updateOne()
B) updateMany()
C) updateAll()
D) replaceMany()

Answer: B) updateMany()

Explanation: The updateMany() method is used to update multiple documents in MongoDB.


48. What will the following query do in MongoDB: db.collection.updateOne({ age: { $gt: 30 } }, { $set: { status: "Senior" } })?

A) Updates all documents where age is greater than 30, setting status to "Senior"
B) Updates one document where age is greater than 30, setting status to "Senior"
C) Replaces one document where age is greater than 30
D) Removes the status field for all documents where age is greater than 30

Answer: B) Updates one document where age is greater than 30, setting status to "Senior"

Explanation: The updateOne() method will update the first document where age is greater than 30 and set the status to "Senior."


49. What is the purpose of using findOneAndUpdate() in MongoDB?

A) To find and update a document in a single operation
B) To find and delete a document
C) To update multiple documents at once
D) To insert a document

Answer: A) To find and update a document in a single operation

Explanation: The findOneAndUpdate() method finds a document and applies the update in one atomic operation.


50. Which operator would you use to ensure that only the field specified in the update is changed in MongoDB?

A) $update
B) $set
C) $unset
D) $push

Answer: B) $set

Explanation: The $set operator ensures that only the specified fields are updated, leaving other fields unaffected.

 

Tags

Post a Comment

0Comments

Post a Comment (0)

About Me