Update-or-Insert: Upserts in MongoDB

A relatively common task in a database-powered application is to update an existing entry, or if not found insert it as a new record. MongoDB conveniently supports this as a single operation, freeing you from having to implement your own “if-exists-update-else-insert” logic. 10gen refer to this type of write operation as an “upsert”.

In PyMongo, there are three possible methods one can use to perform an upsert. These are Collection.save(), Collection.update() and Collection.find_and_modify(). We shall start by describing Collection.save() as it is the most straight forward method.

In the earlier section “Inserting a Document into a Collection” we used the Collection.insert() method to write a new document to the collection. However, we could have just as easily used the save() method. save() offers almost identical functionality to insert() with the following exceptions: save() can perform upserts and save() cannot insert multiple documents in a single call.

save() is quite easy to understand. If you pass it a document without an _id property it will perform an insert(), creating a brand new document. If, on the other hand, you pass it a document which contains an _id property it will update the existing document corresponding to that _id value, overwriting the already-present document with the document passed to save().

This is the essence of an upsert: If a document already exists, update it. Otherwise create a new document.

save() can be useful ...

Get MongoDB and Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.