How do I update a Mongo document after inserting it?

后端 未结 5 606
借酒劲吻你
借酒劲吻你 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:59

    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.

提交回复
热议问题