How do I update a Mongo document after inserting it?

后端 未结 5 600
借酒劲吻你
借酒劲吻你 2020-12-04 13:57

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

5条回答
  •  攒了一身酷
    2020-12-04 14:40

    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)

提交回复
热议问题