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

Method bulk_write

pymongo/synchronous/mongo_client.py:2485–2615  ·  view source on GitHub ↗

Send a batch of write operations, potentially across multiple namespaces, to the server. Requests are passed as a list of write operation instances ( :class:`~pymongo.operations.InsertOne`, :class:`~pymongo.operations.UpdateOne`, :class:`~pymongo.operations.UpdateMan

(
        self,
        models: Sequence[_WriteOp],
        session: Optional[ClientSession] = None,
        ordered: bool = True,
        verbose_results: bool = False,
        bypass_document_validation: Optional[bool] = None,
        comment: Optional[Any] = None,
        let: Optional[Mapping[str, Any]] = None,
        write_concern: Optional[WriteConcern] = None,
    )

Source from the content-addressed store, hash-verified

2483
2484 @_csot.apply
2485 def bulk_write(
2486 self,
2487 models: Sequence[_WriteOp],
2488 session: Optional[ClientSession] = None,
2489 ordered: bool = True,
2490 verbose_results: bool = False,
2491 bypass_document_validation: Optional[bool] = None,
2492 comment: Optional[Any] = None,
2493 let: Optional[Mapping[str, Any]] = None,
2494 write_concern: Optional[WriteConcern] = None,
2495 ) -> ClientBulkWriteResult:
2496 """Send a batch of write operations, potentially across multiple namespaces, to the server.
2497
2498 Requests are passed as a list of write operation instances (
2499 :class:`~pymongo.operations.InsertOne`,
2500 :class:`~pymongo.operations.UpdateOne`,
2501 :class:`~pymongo.operations.UpdateMany`,
2502 :class:`~pymongo.operations.ReplaceOne`,
2503 :class:`~pymongo.operations.DeleteOne`, or
2504 :class:`~pymongo.operations.DeleteMany`).
2505
2506 >>> for doc in db.test.find({}):
2507 ... print(doc)
2508 ...
2509 {'x': 1, '_id': ObjectId('54f62e60fba5226811f634ef')}
2510 {'x': 1, '_id': ObjectId('54f62e60fba5226811f634f0')}
2511 ...
2512 >>> for doc in db.coll.find({}):
2513 ... print(doc)
2514 ...
2515 {'x': 2, '_id': ObjectId('507f1f77bcf86cd799439011')}
2516 ...
2517 >>> # DeleteMany, UpdateOne, and UpdateMany are also available.
2518 >>> from pymongo import InsertOne, DeleteOne, ReplaceOne
2519 >>> models = [InsertOne(namespace="db.test", document={'y': 1}),
2520 ... DeleteOne(namespace="db.test", filter={'x': 1}),
2521 ... InsertOne(namespace="db.coll", document={'y': 2}),
2522 ... ReplaceOne(namespace="db.test", filter={'w': 1}, replacement={'z': 1}, upsert=True)]
2523 >>> result = client.bulk_write(models=models)
2524 >>> result.inserted_count
2525 2
2526 >>> result.deleted_count
2527 1
2528 >>> result.modified_count
2529 0
2530 >>> result.upserted_count
2531 1
2532 >>> for doc in db.test.find({}):
2533 ... print(doc)
2534 ...
2535 {'x': 1, '_id': ObjectId('54f62e60fba5226811f634f0')}
2536 {'y': 1, '_id': ObjectId('54f62ee2fba5226811f634f1')}
2537 {'z': 1, '_id': ObjectId('54f62ee28891e756a6e1abd5')}
2538 ...
2539 >>> for doc in db.coll.find({}):
2540 ... print(doc)
2541 ...
2542 {'x': 2, '_id': ObjectId('507f1f77bcf86cd799439011')}

Callers

nothing calls this directly

Calls 4

InvalidOperationClass · 0.90
_ClientBulkClass · 0.90
_add_to_client_bulkMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected