Reputation: 25
I'm puzzled by a problem I'm having with the Mongo Shell producing different variations on the output.
When I log into the mongo shell and run the simplest of queries, such as:
db.database.findOne();
I get a document returned.
When I invoke the same command through the --eval option on the command line, I get a different result: (xx's redact sensitive info)
mongo -u xxx -p xxx xxxx --eval 'db.database.findOne();'
MongoDB shell version: 3.0.6
connecting to: xxxx
[object Object]
And when I put this command in a file and try it, I get no output at all.
mongo -u xxx-p xxx xxxx example.js
MongoDB shell version: 3.0.6
connecting to: xxxx
(I just get returned to a command line here)
Example.js contains:
db.database.findOne();
What part of the Mongo shell manual did I miss that explains this behavior?
My goal is to be able to feed the output of my mongo query into a shell script.
Upvotes: 2
Views: 1167
Reputation: 2821
When a findOne()
is run within the shell it will print the result in JSON as a convenience. To print the same via shell script or eval you need to print the return value via a printjson()
call:
mongo -u xxx -p xxx xxxx --eval 'printjson(db.database.findOne());'
For more on scripting with the mongo
shell see the official MongoDB Shell docs.
Upvotes: 1