How to update a Mongo document after pasting it?

Say I'm pasting a document.

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

Now let's say I want to add a field and update it. How should I do it? This does not seem to work .....

 post = mycollection.find_one({"_id":mongo_id}) post['newfield'] = "abc" mycollection.save(post) 
+50
python database mongodb pymongo
Dec 07 2018-10-12T00:
source share
5 answers

In pymongo you can update:
mycollection.update({'_id':mongo_id}, {"$set": post}, upsert=False)
The Upsert parameter will be inserted instead of updating if mail is not found in the database.
The documentation can be found on the mongodb website .

UPDATE For version> 3, use update_one instead of updating:

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

+74
Dec 07 '10 at 7:32
source share

I will use collection.save(the_changed_dict) this way. I just checked this and it still works for me. The following is a quote from pymongo doc. :

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

Save the document in this collection.

If to_save already has "_id", then the update () (upsert) operation is performed and any existing document with this "_id" is overwritten. Otherwise, the insert () operation is performed. In this If you manipulate True, then "_id" is added to _ to_save, and this method returns the "_id" of the saved document. If you manipulate False, "_id" will be added by the server, but this method will return Missing.

+20
Aug 14 '12 at 8:32
source share

This is an old question, but I came across this when I was looking for an answer, so I would like to give an update for the answer.

The save and update methods are deprecated.

save (to_save, manipulate = True, check_keys = True, ** kwargs) ΒΆ Save the document in this collection.

DEPRECATED - use insert_one () or replace_one () instead.

Changed in version 3.0: Removed a secure setting. Skip w = 0 for unacknowledged write operations.

update (spec, document, upsert = False, manipulate = False, multi = False, check_keys = True, ** kwargs) Update the document in this collection.

DEPRECATED - use replace_one (), update_one () or update_many () instead.

Changed in version 3.0: Removed a secure setting. Skip w = 0 for unacknowledged write operations.

in the specific case of OPs, it is better to use replace_one .

+6
Aug 20 '15 at 20:47
source share

According to the latest PyMongo documentation called Paste Document (Paste is outdated) and after a protective approach you should paste 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) 
+3
Dec 04 '16 at 12:34
source share
 mycollection.find_one_and_update({"_id": mongo_id}, {"$set": {"newfield": "abc"}}) 

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

+2
Oct 06 '17 at 15:16
source share



All Articles