| 17-May-2020 | Like this? Dislike this? Let me know |
MongoDB provides a command line interface (CLI), conveniently called mongo, much like all other databases or subsystems in general. From this CLI you can perform queries, perform administration, examine stats, etc.:
$ mongo
MongoDB Enterprise rs0:PRIMARY> use testX
switched to db testX
MongoDB Enterprise rs0:PRIMARY> show collections
product
trade
MongoDB Enterprise rs0:PRIMARY> db.trade.count();
63
MongoDB Enterprise rs0:PRIMARY> db.trade.findOne();
{
"_id" : ObjectId("5d2fcc8b983742c7e2b8df12"),
"cid" : "ABC123",
"product" : "A",
"amount" : NumberDecimal("100.000000000000"),
"tdate" : ISODate("2018-01-01T00:00:00Z")
}
$ cat myscript.js
// Whatever db is defaulted upon startup, getSiblingSB() is a way to switch over
// to a new target DB. You must have permissions of course, but this function
// from a javascript basis is effectively "use testX", which is *not* a valid
// javascript expression. "use" and "show" are intercepted by the CLI before
// passing them to the javascript engine. Expressions read from a script,
// however, are passed directly to the javascript engine:
db = db.getSiblingDB("testX");
c = db.trade.find({"product":"D"}).sort({"tdate":-1}).limit(1);
while(c.hasNext()) {
var item = c.next();
printjson(item);
}
$ mongo myscript.js
{
"_id" : ObjectId("5d2fcc8b983742c7e2b8df1d"),
"cid" : "JDU876",
"product" : "D",
"amount" : NumberDecimal("300.000000000000"),
"tdate" : ISODate("2018-03-01T00:00:00Z")
}
$ mongo
MongoDB Enterprise rs0:PRIMARY> load("myscript.js")
{
"_id" : ObjectId("5d2fcc8b983742c7e2b8df1d"),
"cid" : "JDU876",
"product" : "D",
"amount" : NumberDecimal("300.000000000000"),
"tdate" : ISODate("2018-03-01T00:00:00Z")
}
$ cat myscript.js
target = "A"; // default
if(typeof args !== 'undefined') { // defend against no command line args passed via --eval
target = args.product; // pick up product arg
}
db = db.getSiblingDB("testX");
c = db.trade.find({"product":target}).sort({"tdate":-1}).limit(1);
while(c.hasNext()) {
var item = c.next();
printjson(item);
}
$ mongo myscript.js --eval 'args={"product":"D"}'
{
"_id" : ObjectId("5d2fcc8b983742c7e2b8df1d"),
"cid" : "JDU876",
"product" : "D",
"amount" : NumberDecimal("300.000000000000"),
"tdate" : ISODate("2018-03-01T00:00:00Z")
}
$ mongo --host "mongodb://machine:port" --eval 'args={"product":"D"}' myscript.js
$ cat myscript.js
targets = ["A"]; // Ah! Now the default is an *array* of one item, "A"
order = 1; // default
if(typeof args !== 'undefined') {
targets = args.products;
order = args.order;
}
db = db.getSiblingDB("testX");
c = db.trade.find({"product":{$in:targets}}).sort({"tdate":order}); // take limit off...
while(c.hasNext()) {
var item = c.next();
printjson(item);
}
$ mongo myscript.js --eval 'args={"products":["A","D"],order:1}'
{
"_id" : ObjectId("5d2fcc8b983742c7e2b8df12"),
"cid" : "ABC123",
"product" : "A",
"amount" : NumberDecimal("100.000000000000"),
"tdate" : ISODate("2018-01-01T00:00:00Z")
}
{
"_id" : ObjectId("5d2fcc8b983742c7e2b8df15"),
"cid" : "ABC123",
"product" : "D",
"amount" : NumberDecimal("98.000000000000"),
"tdate" : ISODate("2018-01-01T00:00:00Z")
}
...
For example, how might we pass a penny-precise decimal arg on the command line? Assuming we change the script and the call to find to this:
vamt = new NumberDecimal("100");
if(typeof args !== 'undefined') {
if(typeof args.products !== 'undefined') { products = args.products; }
if(typeof args.order !== 'undefined') { order = args.order; }
if(typeof args.amount !== 'undefined') { vamt = args.amount; }
}
c = db.trade.find({"product":{$in:products} , amount:{$lt:vamt} } ).sort({"tdate":order});
$ mongo myscript.js --eval 'args={"products":["A","D"],order:1,amount:new NumberDecimal("100")}'
{
"_id" : ObjectId("5d2fcc8b983742c7e2b8df15"),
"cid" : "ABC123",
"product" : "D",
"amount" : NumberDecimal("98.000000000000"),
"tdate" : ISODate("2018-01-01T00:00:00Z")
}
...
$ mongo myscript.js --eval 'args={"products":["A","D"],order:1,amount:new NumberDecimal("100"),startDate:new ISODate("2020-02-01")}'
BAD! Both amount and startDate will be presented in myscript.js as strings; without
new, an object is constructed and toString() called!
$ mongo myscript.js --eval 'args={"products":["A","D"],order:1,amount:NumberDecimal("100"),startDate:ISODate("2020-02-01")}'
CORRECT! Use new:
$ mongo myscript.js --eval 'args={"products":["A","D"],order:1,amount:new NumberDecimal("100"),startDate:new ISODate("2020-02-01")}'
// DEFAULTS:
products = ["A"]; // default
order = -1;
if(typeof args !== 'undefined') {
if(typeof args.products !== 'undefined') { products = args.products; }
if(typeof args.order !== 'undefined') { order = args.order; }
}
Like this? Dislike this? Let me know