mongodb script types
Monday, April 23, 2012
There's the obvious choice of client drivers in your language of choice for application-based mongodb connections, but for server-side admin and scripting, js is best. But from the shell and cron, often they get wrapped in shell scripts. This table-esque post outlines some of the decision points to use when approaching this, plus some guidance as to how to write them.
Interactive shell
Purpose: ad-hoc queries
Syntax:mongo
About:
Syntax:mongo
About:
The most-used type of query. Not suited to automated tasks. Commands are processed immediately and results returned to the console session. Docs
--eval
Purpose: in-line queries from script or cron
Syntax: mongo dbname --eval "db.coll..."
About:
Syntax: mongo dbname --eval "db.coll..."
About:
Great for one-liners, eg cron tasks
- see printjson
js file
Purpose: call javascript from file because it's long or common to different purposes
Syntax: mongo dbname filename.js
About:
Syntax: mongo dbname filename.js
About:
Include all needed code in js file. Usually not suitable when output needed. Best for maintenance tasks longer than one line.
eval + js file
Purpose: call javascript from file when common to different purposes
Syntax: mongo dbname --eval "param1=val1;param2=val2" filename.js
About:
Syntax: mongo dbname --eval "param1=val1;param2=val2" filename.js
About:
This will execute the mongodb js in filename.js, but will have available to it any values as declared in the eval string. Can be called from a shell script which can modify the eval values and capture / format output
All within one sh script
Purpose: singular, self-contained script, not intended for multi-purposes. Eg monitoring
Syntax:
Syntax:
mongo _dbname_ \-\-eval "param1=val1;param2=val2" <<eof
js here
js here
eof
js here
js here
eof
About:
Allows running map/reduce commands, as it is treated as a standalone js file by the use of HERE Documents. Can also capture the output and format it.
Gotcha Any shell keywords or characters will need to be escaped in the HERE Document otherwise they'll be interpreted and the result passed. Eg "$gt" (mongodb greater-than) otherwise "$gt" will be evaluated and null passsed if the variable isn't set.
map/reduce:
- Cannot be run inside an --eval
- From mapreduce docs { inline : 1} - With this option, no collection will be created, and the whole map-reduce operation will happen in RAM. Also, the results of the map-reduce will be returned within the result object. Note that this option is possible only when the result set fits within the 16MB limit of a single document. In v2.0, this is your only available option on a replica set secondary.
- Queries (such as count(), sort()) cannot be run on inline results (as of mongodb 2.1+ new aggregation features may allow this). For versions <2.1, seeing as cannot run these queries on inline results, and can't have temp collections on secondary servers, have to run these on primary server so can save to a temp collection and run queries against that.
Subscribe to:
Posts (Atom)