来源:https://pymongo.readthedocs.io/en/stable/api/pymongo/asynchronous/collection.html
0. 更新文档的 options
-
$inc
数字增加db.test.update_one({'x': 1}, {'$inc': {'x': 3}})
-
$set
设置指定的 fielddb.test.update_one({'x': 1}, {'$set': {'y': 3}})
-
$push
adds elements to the end of an array if the array exists and creates a new array if it does not -
$ne
You might want to treat an array as a set, only adding values if they are not present. This can be done using "$ne" in the query document. This can also be done with "$addToSet
", which is useful for cases where "$ne" won’t work or where "$addToSet" describes what is happening better.
1. 更新一个文档
函数:
- update_one
async update_one(filter, update, upsert=False, bypass_document_validation=False, collation=None, array_filters=None, hint=None, session=None, let=None, comment=None)
update_many
async update_many(filter, update, upsert=False, array_filters=None, bypass_document_validation=None, collation=None, hint=None, session=None, let=None, comment=None)
使用方式
result = await db.test.update_one({'x': 1}, {'$inc': {'x': 3}})
或者
result = db.test.update_one({'x': 1}, {'$inc': {'x': 3}})