Search code examples
pythonmongodbpymongobulkbulk-delete

Pymongo Bulk Delete


I need to use pymongo to do bulk delete for mongodb. I'm getting the _id field of the documents that I need deleted with a query but I'm not able to figure out how to use the _id's I get to be deleted in chunks of 10,000.


Solution

  • Below is an example of Unordered Bulk Write Operations using current PyMongo v3.7.2:

    from pymongo import DeleteOne
    from pymongo.errors import BulkWriteError
    
    requests = [
            DeleteOne({'_id': 101}),
            DeleteOne({'_id': 102})]
    try:
        db.collection.bulk_write(requests, ordered=False)
    except BulkWriteError as bwe:
        pprint(bwe.details)
    

    The example above is using the unordered operations, because unordered bulk operations are batched and sent to the server in arbitrary order where they may be executed in parallel. Any errors that occur are reported after all operations are attempted. See also PyMongo Bulk Write Operations and MongoDB Bulk Write Operations for more information.