1.0.0 • Published 5 years ago
process-collection v1.0.0
Process Collection
Run a function on documents in collections asyncronously and with progress indicator.
Setup
Install dependencies:
npm installTo run the test example:
makeThis assumes you have mongo running on localhost port 3001, with a database called meteor that contains a clients collection.
The makefile directs standard output to output.log while displaying process to stderr.
processCollection
This function has a single argument with properties:
collection: mongo collectionquery: a mongo query on the collectionprojection: projection to limit the fields (might be broken, but would be helpful for large documents over the network)sort: sortlimit: limitshowProgress: boolean, default true: output progress with eta to stderrhandler: handler function that takes a single document
example:
const connect = require('./lib/connection')
const processCollection = require('./lib/process-collection')
connect('mongodb://localhost:3001/meteor').then(async client => {
const Clients = client.db().collection('clients')
await processCollection({
collection: Clients,
query: { created_at: { $gte: new Date("2016-01-01" ) } },
handler: function(doc) {
console.log(doc._id);
}
})
client.close()
})The above will output the _id of each document in the clients collection from the meteor database on localhost:3001.
1.0.0
5 years ago