0

I'm trying to execute a mongodb query in a single command so I can run a batch script from within windows software. The mongodb database is already built, here are my windows console commands and cli query that work:

C:\Program Files\MongoDB\Server\3.0\bin>mongo ds045565-a1.mongolab.com:45565/heroku_mydatabase
2017-09-01T09:14:05.196+0100 I CONTROL  Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 3.0.7
connecting to: ds045565-a1.mongolab.com:45565/heroku_mydatabase
rs-ds045565:PRIMARY> db.auth("myusername","mypassword");db.getCollection('94081_DS_FuelNBS').find({})
{ "_id" : ObjectId("58ab4ae3bc5cf82663236b4f"), "code" : "D", "description" : "Diesel", "lastModified" : ISODate("2017-02-20T20:00:35.680Z") }
{ "_id" : ObjectId("58ab4ae3bc5cf82663236b50"), "code" : "H", "description" : "Hydrogen", "lastModified" : ISODate("2017-02-20T20:00:35.680Z") }
{ "_id" : ObjectId("58ab4ae3bc5cf82663236b51"), "code" : "P", "description" : "Petrol", "lastModified" : ISODate("2017-02-20T20:00:35.680Z") }
rs-ds045565:PRIMARY> exit
bye

If I then try to execute as a single windows command I do not get any response output other than the error messages:

C:\Program Files\MongoDB\Server\3.0\bin>mongo ds045565-a1.mongolab.com:45565/heroku_mydatabase mymongodbcommands.txt > output.txt

C:\Program Files\MongoDB\Server\3.0\bin>

Here is the contents of mymongodbcommands.txt and output.txt

mymongodbcommands.txt:

db.auth("myusername","mypassword");db.getCollection('94081_DS_FuelNBS').find({})

output.txt

2017-09-01T09:15:41.604+0100 I CONTROL  Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 3.0.7
connecting to: ds045565-a1.mongolab.com:45565/heroku_mydatabase

1 Answers1

0

Please consider Scripts for Mongo Shell - this is correct way of doing what you want

As example for your case:

mongo ds045565-a1.mongolab.com:45565/heroku_mydatabase -u myusername -p mypassword --eval "var c = db.getCollection('94081_DS_FuelNBS').find(); while(c.hasNext()) {printjson(c.next())}"

You can further build your own mymongodbcommands.js and pass it like:

mongo host:port/db mymongodbcommands.js > output.txt

whereas js file will contain both authentication and your desired bunch of commands

See also this and that answers to similar question.

Kostiantyn
  • 1,856
  • 11
  • 13
  • Thanks Kostiantyn, this now just appears to spit out js code!.... 2017-09-01T10:00:03.343+0100 I CONTROL Hotfix KB2731284 or later update is not installed, will zero-out data files MongoDB shell version: 3.0.7 connecting to: ds045565-a1.mongolab.com:45565/heroku_ltpz6v5f { "_mongo" : connection to ds045565-a1.mongolab.com:45565, "_db" : heroku_ltpz6v5f, "_collection" : heroku_ltpz6v5f.94081_DS_FuelNBS, "_ns" : "heroku_ltpz6v5f.94081_DS_FuelNBS", "_query" : { }, "_fields" : null, "_limit" : 0, "_skip" : 0, – Toby Martin Smith Sep 01 '17 at 09:01
  • Looks like syntax of commands passed to eval is not correct. Try `-- eval "var c = db.getCollection('94081_DS_FuelNBS').find(); while(c.hasNext()) {printjson(c.next())}"` I've updated answer accordingly – Kostiantyn Sep 01 '17 at 09:39
  • It works, you are a genius and your boss should promote you. Many thanks – Toby Martin Smith Sep 01 '17 at 10:16