3
votes

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.

1
Please include some code and maybe what the db looks like so we can help you better. Have you also tried looking at the docs? possibly deleteMany() would be down your alley.Jab

1 Answers

4
votes

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.