0
0
MongoDBquery~5 mins

Upsert behavior (update or insert) in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does 'upsert' mean in MongoDB?
Upsert means to update a document if it exists, or insert a new document if it does not exist.
Click to reveal answer
beginner
Which MongoDB method supports upsert behavior?
The updateOne(), updateMany(), and findOneAndUpdate() methods support upsert when you set the option { upsert: true }.
Click to reveal answer
beginner
What option must be set to true to enable upsert in an update operation?
The option 'upsert: true' must be set in the update operation options.
Click to reveal answer
intermediate
If no document matches the filter in an upsert operation, what happens?
A new document is created using the filter and update fields combined, then inserted into the collection.
Click to reveal answer
intermediate
Can upsert cause duplicate documents if the filter is not unique?
Yes, if the filter does not uniquely identify documents, multiple inserts can happen on repeated upserts.
Click to reveal answer
What does setting { upsert: true } do in a MongoDB update operation?
AOnly updates existing documents
BDeletes the matched document
CInserts a new document if no match is found
DReturns an error if no document matches
Which method can perform an upsert in MongoDB?
Afind()
BupdateOne()
CdeleteOne()
DinsertOne()
If a document matches the filter in an upsert operation, what happens?
AThe document is updated
BA new document is inserted
CThe document is deleted
DNothing happens
What happens if the filter in an upsert is not unique?
AThe operation is ignored
BOnly one document is updated
CAn error is thrown
DMultiple documents may be inserted on repeated upserts
Which option is required to perform an upsert with findOneAndUpdate()?
A{ upsert: true }
B{ multi: true }
C{ new: true }
D{ returnOriginal: true }
Explain how upsert works in MongoDB and when you would use it.
Think about saving time by combining update and insert in one step.
You got /4 concepts.
    Describe the risks of using upsert with a non-unique filter.
    Consider what happens if MongoDB can't find a unique match.
    You got /4 concepts.