3

I am using the current mongosh tool to run Mongo queries which may return very large result sets, which might not fit within the buffer of the command prompt (and even if they did, copying such results by hand would be time consuming and error prone). Therefore, I would like to have the ability to:

  • pipe the output from mongosh to a text file, and
  • also pass in a Mongo script or query to run from the command line

Here is the command I am currently running (details masked):

mongosh "mongodb://10.20.30.40:26017" --username my_username --password my_password
    --authenticationDatabase my_database > output.txt

This is the current output I see in output.txt:

Current sessionID:  1ee96e0f6025ec328ea25ccc
Connecting to:      mongodb://10.20.30.40:26017
Using MongoDB:      3.4.13
Using Mongosh Beta: 0.0.6

For more information about mongosh, please see our docs: https://docs.mongodb.com/mongodb-shell/

[1G[0J> [3G

So it seems that redirection to the output file is working. However, I can't seem to figure out how to specify a Mongo script with an actual query to the command line tool. For example, if I wanted to specify the following simple count query, how would I do it?

db.getCollection('my_collection').count();

Ideally, I would like output.txt to just have a single line with the count from this collection.

The earlier mongo command line tool seemed to have a number of ways of piping in a query, e.g. by specifying a script or using the --eval option. The documentation for mongosh claims that this tool is in Beta mode and supports some subset of mongo functionality. Does it support the ability to pass a query?

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360

4 Answers4

1

Meanwhile this also works with new mongosh, ensure you downloaded the latest version (1.1.9 or newer)

echo "db.getCollection('my_collection').countDocuments({});" | mongosh --norc --quiet > output.txt

mongosh --norc --quiet > output.txt <<EOF
db.getCollection('my_collection').countDocuments({});
EOF

mongosh --norc --quiet --eval "db.getCollection('my_collection').countDocuments({})" > output.txt

Note, with classic mongo shell you get this warning:

================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================

which is not true in my opinion, I don't see new mongosh as successor for mongosh, yet. But I have to admit, it is getting better and better.

The only way to get rid of this warning is the --eval option.

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
0

The following works in mongo (I am yet to try it with mongosh)

mongo --eval "db=db.getSiblingDB('DB NAME'); printjson(db.COLLECTION.findOne())" > output.txt --quiet

So yours would become:

mongo "mongodb://10.20.30.40:26017" --username my_username --password my_password --authenticationDatabase my_database --eval "db=db.getSiblingDB('my_database'); printjson(db.my_collection.count())" > output.txt --quiet
Yahya
  • 3,386
  • 3
  • 22
  • 40
  • `I am yet to try it with mongosh` ... I need a solution for `mongosh`. – Tim Biegeleisen Jan 20 '21 at 09:29
  • Only "data" apparent in the `output.txt` file is `[1G[0J> [3G` ... maybe this is some weird encoding, or maybe gibberish, but your second option is not working for me. – Tim Biegeleisen Jan 20 '21 at 09:44
  • Correct, it might have changed in the new version. Documentation says, you can do --eval, while help says otherwise. – Yahya Jan 20 '21 at 10:59
  • From help page mongosh does not support option `--eval` as of my version 0.5.2. However in current doc [mongosh Options](https://docs.mongodb.com/mongodb-shell/reference/options) it seems to be available. – Wernfried Domscheit Jan 20 '21 at 11:01
  • @TimBiegeleisen this is a bug, which is reported to MongoDB now. – Yahya Jan 20 '21 at 13:12
  • You can try: `mongosh --eval "..." --norc --quiet` and that should work too – buzypi Dec 02 '22 at 05:56
0

As mentionned in the mongoDB Documentation, You can write your js script and use it with the --file option in the commandline.

mongosh --norc --quiet --file "test_script.js" > output.txt

to write the output of queries like : db.<collection_name>.findOne();

Use this syntax in your jscript: printjson ( db.<collection_name>.findOne() );

more details: https://www.mongodb.com/docs/mongodb-shell/write-scripts/

0

My colleague Cedric provided this solution which worked to capture the output completely

Of note are

  • The "your_execution_db" besides the user name.
  • More importantly, your .js script SHOULD NOT have a USE statement inside it! If it has a USE statement, the output gets suppressed!

This format captures the output inside the .js script such as:

  • print ("hello world");
  • printjson({"UserId":101,"UserName":"John","UserCoreSuject":["Java","MongoDB","MySQL","SQL Server"]});
  • console.log('Getting jobs collection...'); console.log(db.getCollection('jobs'));

Syntax:

mongosh.exe --norc --host=ReplicaSetHostName1.comp.com:27000,ReplicaSetHostName2.comp.com:27000,ReplicaSetHostName3.comp.com:27000 --username=your_user your_execution_db --password MySuperSecurePassword --authenticationDatabase=admin_or_your_auth_db --file "C:\Temp\MyScript.js" -quiet > C:\Temp\MyScript.js.log