Update one or more documents that match the filter. >>> for doc in db.test.find(): ... print(doc) ... {'x': 1, '_id': 0} {'x': 1, '_id': 1} {'x': 1, '_id': 2} >>> result = db.test.update_many({'x': 1}, {'$inc': {'x': 3}})
(
self,
filter: Mapping[str, Any],
update: Union[Mapping[str, Any], _Pipeline],
upsert: bool = False,
array_filters: Optional[Sequence[Mapping[str, Any]]] = None,
bypass_document_validation: Optional[bool] = None,
collation: Optional[_CollationIn] = None,
hint: Optional[_IndexKeyHint] = None,
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
)
| 1351 | ) |
| 1352 | |
| 1353 | def update_many( |
| 1354 | self, |
| 1355 | filter: Mapping[str, Any], |
| 1356 | update: Union[Mapping[str, Any], _Pipeline], |
| 1357 | upsert: bool = False, |
| 1358 | array_filters: Optional[Sequence[Mapping[str, Any]]] = None, |
| 1359 | bypass_document_validation: Optional[bool] = None, |
| 1360 | collation: Optional[_CollationIn] = None, |
| 1361 | hint: Optional[_IndexKeyHint] = None, |
| 1362 | session: Optional[ClientSession] = None, |
| 1363 | let: Optional[Mapping[str, Any]] = None, |
| 1364 | comment: Optional[Any] = None, |
| 1365 | ) -> UpdateResult: |
| 1366 | """Update one or more documents that match the filter. |
| 1367 | |
| 1368 | >>> for doc in db.test.find(): |
| 1369 | ... print(doc) |
| 1370 | ... |
| 1371 | {'x': 1, '_id': 0} |
| 1372 | {'x': 1, '_id': 1} |
| 1373 | {'x': 1, '_id': 2} |
| 1374 | >>> result = db.test.update_many({'x': 1}, {'$inc': {'x': 3}}) |
| 1375 | >>> result.matched_count |
| 1376 | 3 |
| 1377 | >>> result.modified_count |
| 1378 | 3 |
| 1379 | >>> for doc in db.test.find(): |
| 1380 | ... print(doc) |
| 1381 | ... |
| 1382 | {'x': 4, '_id': 0} |
| 1383 | {'x': 4, '_id': 1} |
| 1384 | {'x': 4, '_id': 2} |
| 1385 | |
| 1386 | :param filter: A query that matches the documents to update. |
| 1387 | :param update: The modifications to apply. |
| 1388 | :param upsert: If ``True``, perform an insert if no documents |
| 1389 | match the filter. |
| 1390 | :param bypass_document_validation: If ``True``, allows the |
| 1391 | write to opt-out of document level validation. Default is |
| 1392 | ``False``. |
| 1393 | :param collation: An instance of |
| 1394 | :class:`~pymongo.collation.Collation`. |
| 1395 | :param array_filters: A list of filters specifying which |
| 1396 | array elements an update should apply. |
| 1397 | :param hint: An index to use to support the query |
| 1398 | predicate specified either by its string name, or in the same |
| 1399 | format as passed to |
| 1400 | :meth:`~pymongo.collection.Collection.create_index` (e.g. |
| 1401 | ``[('field', ASCENDING)]``). This option is only supported on |
| 1402 | MongoDB 4.2 and above. |
| 1403 | :param session: a |
| 1404 | :class:`~pymongo.client_session.ClientSession`. |
| 1405 | :param let: Map of parameter names and values. Values must be |
| 1406 | constant or closed expressions that do not reference document |
| 1407 | fields. Parameters can then be accessed as variables in an |
| 1408 | aggregate expression context (e.g. "$$var"). |
| 1409 | :param comment: A user-provided comment to attach to this |
| 1410 | command. |