A simple Node.js script that finds the top X largest documents in a given MongoDB collection.
- Scalable - can work with any collection size
- Fast - utilizes range-based pagination for fast table scanning
- Easy - simply provide the MongoDB connection string and collection name
- A MongoDB database
- Node.js v4.x+ for ES6 generators support
First, install the package using npm:
npm install mongodb-largest-documents --save
Then, use the following code to find the top 100 largest documents in a given collection, modifying the config
variable accordingly:
var mongodbLargestDocuments = require('mongodb-largest-documents');
// Configure task here
var config = {
// MongoDB connection string
db: 'localhost/test',
// Name of collection to inspect
collectionName: 'users',
// Process X items every iteration
batchSize: 100,
// Limit output to X largest documents
outputTopXLargest: 100
};
// Run the module
mongodbLargestDocuments(config, function (err, result) {
// Handle errors
if (err) {
return console.log(err);
}
// Print largest documents to console
console.log('Largest documents:', result);
});
Run the script and watch the console for progress - it will invoke your callback when it traverses the entire collection.
Apache 2.0