How do I update a Mongo document after inserting it?

PythonMongodbPymongoDatabase

Python Problem Overview


Let's say I insert the document.

post = { some dictionary }
mongo_id = mycollection.insert(post)

Now, let's say I want to add a field and update it. How do I do that? This doesn't seem to work.....

post = mycollection.find_one({"_id":mongo_id}) 
post['newfield'] = "abc"
mycollection.save(post)

Python Solutions


Solution 1 - Python

In pymongo you can update with:

mycollection.update({'_id':mongo_id}, {"$set": post}, upsert=False)

Upsert parameter will insert instead of updating if the post is not found in the database.
Documentation is available at mongodb site.

UPDATE For version > 3 use update_one instead of update:

mycollection.update_one({'_id':mongo_id}, {"$set": post}, upsert=False)

Solution 2 - Python

mycollection.find_one_and_update({"_id": mongo_id}, 
                                 {"$set": {"newfield": "abc"}})

should work splendidly for you. If there is no document of id mongo_id, it will fail, unless you also use upsert=True. This returns the old document by default. To get the new one, pass return_document=ReturnDocument.AFTER. All parameters are described in the API.

The method was introduced for MongoDB 3.0. It was extended for 3.2, 3.4, and 3.6.

Solution 3 - Python

I will use collection.save(the_changed_dict) this way. I've just tested this, and it still works for me. The following is quoted directly from pymongo doc.:

save(to_save[, manipulate=True[, safe=False[, **kwargs]]])

> Save a document in this collection. > > If to_save already has an "_id" then > an update() (upsert) operation is performed and > any existing document with that "_id" is > overwritten. Otherwise an insert() operation is performed. In this > case if manipulate is True an "_id" will be added to to_save and this > method returns the "_id" of the saved document. If manipulate is False > the "_id" will be added by the server but this method will return > None.

Solution 4 - Python

According to the latest documentation about PyMongo titled Insert a Document (insert is deprecated) and following defensive approach, you should insert and update as follows:

result = mycollection.insert_one(post)
post = mycollection.find_one({'_id': result.inserted_id})

if post is not None:
    post['newfield'] = "abc"
    mycollection.save(post)

Solution 5 - Python

This is an old question, but I stumbled onto this when looking for the answer so I wanted to give the update to the answer for reference.

The methods save and update are deprecated.

> save(to_save, manipulate=True, check_keys=True, **kwargs)¶ Save a > document in this collection. > > DEPRECATED - Use insert_one() or replace_one() instead. > > Changed in version 3.0: Removed the safe parameter. Pass w=0 for > unacknowledged write operations. > > update(spec, document, upsert=False, manipulate=False, multi=False, > check_keys=True, **kwargs) Update a document(s) in this collection. > > DEPRECATED - Use replace_one(), update_one(), or update_many() > instead. > > Changed in version 3.0: Removed the safe parameter. Pass w=0 for > unacknowledged write operations.

in the OPs particular case, it's better to use replace_one.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTIMEXView Question on Stackoverflow
Solution 1 - PythonallaitView Answer on Stackoverflow
Solution 2 - Pythonserv-incView Answer on Stackoverflow
Solution 3 - PythonAndrew_1510View Answer on Stackoverflow
Solution 4 - PythonGürol CanbekView Answer on Stackoverflow
Solution 5 - PythonThinkBonoboView Answer on Stackoverflow