MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / update_one

Method update_one

pymongo/asynchronous/collection.py:1234–1352  ·  view source on GitHub ↗

Update a single document matching the filter. >>> async for doc in db.test.find(): ... print(doc) ... {'x': 1, '_id': 0} {'x': 1, '_id': 1} {'x': 1, '_id': 2} >>> result = await db.test.update_one({'x': 1}, {'$inc': {'x': 3}}

(
        self,
        filter: Mapping[str, Any],
        update: Union[Mapping[str, Any], _Pipeline],
        upsert: bool = False,
        bypass_document_validation: Optional[bool] = None,
        collation: Optional[_CollationIn] = None,
        array_filters: Optional[Sequence[Mapping[str, Any]]] = None,
        hint: Optional[_IndexKeyHint] = None,
        session: Optional[AsyncClientSession] = None,
        let: Optional[Mapping[str, Any]] = None,
        sort: Optional[Mapping[str, Any]] = None,
        comment: Optional[Any] = None,
    )

Source from the content-addressed store, hash-verified

1232 )
1233
1234 async def update_one(
1235 self,
1236 filter: Mapping[str, Any],
1237 update: Union[Mapping[str, Any], _Pipeline],
1238 upsert: bool = False,
1239 bypass_document_validation: Optional[bool] = None,
1240 collation: Optional[_CollationIn] = None,
1241 array_filters: Optional[Sequence[Mapping[str, Any]]] = None,
1242 hint: Optional[_IndexKeyHint] = None,
1243 session: Optional[AsyncClientSession] = None,
1244 let: Optional[Mapping[str, Any]] = None,
1245 sort: Optional[Mapping[str, Any]] = None,
1246 comment: Optional[Any] = None,
1247 ) -> UpdateResult:
1248 """Update a single document matching the filter.
1249
1250 >>> async for doc in db.test.find():
1251 ... print(doc)
1252 ...
1253 {'x': 1, '_id': 0}
1254 {'x': 1, '_id': 1}
1255 {'x': 1, '_id': 2}
1256 >>> result = await db.test.update_one({'x': 1}, {'$inc': {'x': 3}})
1257 >>> result.matched_count
1258 1
1259 >>> result.modified_count
1260 1
1261 >>> async for doc in db.test.find():
1262 ... print(doc)
1263 ...
1264 {'x': 4, '_id': 0}
1265 {'x': 1, '_id': 1}
1266 {'x': 1, '_id': 2}
1267
1268 If ``upsert=True`` and no documents match the filter, create a
1269 new document based on the filter criteria and update modifications.
1270
1271 >>> result = await db.test.update_one({'x': -10}, {'$inc': {'x': 3}}, upsert=True)
1272 >>> result.matched_count
1273 0
1274 >>> result.modified_count
1275 0
1276 >>> result.upserted_id
1277 ObjectId('626a678eeaa80587d4bb3fb7')
1278 >>> await db.test.find_one(result.upserted_id)
1279 {'_id': ObjectId('626a678eeaa80587d4bb3fb7'), 'x': -7}
1280
1281 :param filter: A query that matches the document to update.
1282 :param update: The modifications to apply.
1283 :param upsert: If ``True``, perform an insert if no documents
1284 match the filter.
1285 :param bypass_document_validation: (optional) If ``True``, allows the
1286 write to opt-out of document level validation. Default is
1287 ``False``.
1288 :param collation: An instance of
1289 :class:`~pymongo.collation.Collation`.
1290 :param array_filters: A list of filters specifying which
1291 array elements an update should apply.

Callers

nothing calls this directly

Calls 3

_update_retryableMethod · 0.95
UpdateResultClass · 0.90
_write_concern_forMethod · 0.80

Tested by

no test coverage detected