Use limit on mongodb API

I would like to remove a large number of old documents from one collection, and therefore it makes sense to use an api array. Removing them is as simple as:

var bulk = db.myCollection.initializeUnorderedBulkOp();

bulk.find({
  _id: {
    $lt: oldestAllowedId
  }
}).remove();

bulk.execute();

The only problem is trying to delete every document that meets these criteria, and in this case it’s millions of documents, so for performance reasons I don’t want to delete them right away. I want to use a constraint for an operation so that I can do something like this bulk.limit(10000).execute();and run through the operations for a few seconds to prevent the database from being locked for longer than necessary. However, I could not find any parameters that could be passed in bulk to limit the number that it executes.

Is there a way to limit mass operations this way?

- , , 1000 , , . , .

+4
1

_id , , .forEach. .distinct(). "bulk" .

var bulk = db.myCollection.initializeUnorderedBulkOp();
var count = 0;

var ids = db.myCollection.distinct('_id', { '_id': { '$lt': oldestAllowedId } } );

ids.forEach(function(id) {
    bulk.find( { '_id': id } ).removeOne();
    count++;
    if (count % 1000 === 0) {
        // Execute per 1000 operations and re-init
        bulk.execute();
        // Here you can sleep for a while 
        bulk = db.myCollection.initializeUnorderedBulkOp();
    }
});

// clean up queues
if (count > 0 ) {
    bulk.execute();
}
0

Source: https://habr.com/ru/post/1626585/


All Articles