a small command line tool to execute arbitrary commands on selected documents in one mongodb database collection
version: 0.1.10
$ mongoexec <namespace> <execution> [ <selector> ... ]
whereas:
- <namespace> is
<database>.<collection> - <execution> is the name of an "execution" script (see below for more)
- <selector> is a db.collection.find() expression or the string representation of a documents _id
if no selector is given on command line, read these from stdin. in fact that gives the ability to combine mongoexecs (even on discrete databases) as in:
$ mongoexec products.answers ./get_id.js '{"price":{"$gt":42}}' | mongoexec products.answers ./reduce_price_by_ten_percent.js
obviously this serves as an example only, as it can be achieved shorter (on the same database) as:
$ mongoexec products.answers ./reduce_price_by_ten_percent.js '{"price":{"$gt":42}}'
selectors are db.collection.find() compatible expressions (encoded as JSON) or the hexadecimal string representation of a documents _id
select "active" documents:
{"status" : "active"}select documents named "foo*"
{"name" : {"$regex" : "^foo"}}select this specific document by it's _id (string)
5890935df162533fea1b49eeexecution script are node.js modules following this basic pattern:
module.exports={exec: function(document,collection,callback){varerror=null;varresponse=null;// do something here with the document// use collection to execute commandscallback(error,response);}}execution scripts are loaded with absolute path or relative to current working directory and .js file extension is required
$ mongoexec inventory.cars ./get_license_plate.js '{ "firstLicenseYear" : { "$lt" : "2016" }'
sample: printing the documents _id property:
module.exports={exec: function(document,collection,callback){console.log(document._id);varerror=null;varresponse={_id: document._id};callback(error,response);}}sample: delete a document
module.exports={exec: function(document,collection,callback){// remove by document _id, check for error and responsecollection.deleteOne({_id: document._id},function(err,obj){if(err)callback(err,null);elsecallback(null,{removed: {_id: document._id}});});}};sample: update a (status) property
module.exports={exec: function(document,collection,callback){// update by document _id and responsecollection.updateOne({'_id': document._id},{$set: {'status': 'archived'}},{upsert: false},function(err,r){if(err){console.error(document._id,err);callback(err,null);}else{console.info(document._id,'archived');callback(null,{updated: document._id,to: 'archived'});}});}};$ npm install --global mongoexec.js
$ mongoexec
- none
- eventualy this should also be a npm module package (main: ./lib/...)
- read executions (and? selectors) from file
- swap order of command line arguments? (to: selector execution)
- take decision on final stdin handling (see the options in the code)
- pass db instead or additionaly to collection into the execution?
- pass document as an object with self-saving, updating etc. methods?
- command line options: --verbose, --help, --version
- command line option: --dryrun (how can this be achieved? any handy driver option?)
- transport arguments to execution scripts?
- more modes than exec? (e.g. create)