ChatGPT解决这个技术问题 Extra ChatGPT

Printing Mongo query output to a file while in the mongo shell

2 days old with Mongo and I have a SQL background so bear with me. As with mysql, it is very convenient to be in the MySQL command line and output the results of a query to a file on the machine. I am trying to understand how I can do the same with Mongo, while being in the shell

I can easily get the output of a query I want by being outside of the shell and executing the following command:

mongo localhost:27017/dbname --eval "printjson(db.collectionName.findOne())" > sample.json

The above way is fine, but it requires me to exit the mongo shell or open a new terminal tab to execute this command. It would be very convenient if I could simply do this while still being inside the shell.

P.S: the Question is an offshoot of a question I posted on SO


C
Community

AFAIK, there is no a interactive option for output to file, there is a previous SO question related with this: Printing mongodb shell output to File

However, you can log all the shell session if you invoked the shell with tee command:

$ mongo | tee file.txt
MongoDB shell version: 2.4.2
connecting to: test
> printjson({this: 'is a test'})
{ "this" : "is a test" }
> printjson({this: 'is another test'})
{ "this" : "is another test" }
> exit
bye

Then you'll get a file with this content:

MongoDB shell version: 2.4.2
connecting to: test
> printjson({this: 'is a test'})
{ "this" : "is a test" }
> printjson({this: 'is another test'})
{ "this" : "is another test" }
> exit
bye

To remove all the commands and keep only the json output, you can use a command similar to:

tail -n +3 file.txt | egrep -v "^>|^bye" > output.json

Then you'll get:

{ "this" : "is a test" }
{ "this" : "is another test" }

sigh, I wish this was possible, oh well, thanks for your answer :)
Good answer, but using the cursor keys distorts the output in tmux, to the point where it's pretty much "paste a command, CTRL+C "
m
mPrinC

We can do it this way -

mongo db_name --quiet --eval 'DBQuery.shellBatchSize = 2000; db.users.find({}).limit(2000).toArray()' > users.json

The shellBatchSize argument is used to determine how many rows is the mongo client allowed to print. Its default value is 20.


Unfortunately this is not safe, other useless junk is captured too with some ssl setups: jira.mongodb.org/browse/SERVER-23810
New versions of mongo shell deprecated shellbatchSize. The new syntax is: mongo db_name --quiet --eval 'config.set("displayBatchSize", 2000); db.users.find({}).limit(2000).toArray()' > users.json
R
Rondo

If you invoke the shell with script-file, db address, and --quiet arguments, you can redirect the output (made with print() for example) to a file:

mongo localhost/mydatabase --quiet myScriptFile.js > output 

I came here looking for --quiet specifically, thanks
u
user3504575

There are ways to do this without having to quit the CLI and pipe mongo output to a non-tty.

To save the output from a query with result x we can do the following to directly store the json output to /tmp/x.json:

> EDITOR="cat > /tmp/x.json"
> x = db.MyCollection.find(...).toArray()
> edit x
>

Note that the output isn't strictly Json but rather the dialect that Mongo uses.


why not just EDITOR="cat > x.json"
I didn't test if redirection worked. It seems that works just as well. Nice catch!
For Windows, replace cat with type. (writeFile function also works)
is there something similar for mongosh? when I do edit x it complains about a missing semicolon before x.
FYI if you use let x you'll get SyntaxError: redeclaration of let x when you use the edit command... still works though
k
kris

It may be useful to you to simply increase the number of results that get displayed

In the mongo shell > DBQuery.shellBatchSize = 3000

and then you can select all the results out of the terminal in one go and paste into a text file.

It is what I am going to do :)

(from : https://stackoverflow.com/a/3705615/1290746)


S
SergO

Combining several conditions:

write mongo query in JS file and send it from terminal

switch/define a database programmatically

output all found records

cut initial output lines

save the output into JSON file

myScriptFile.js

// Switch current database to "mydatabase"
db = db.getSiblingDB('mydatabase');

// The mark for cutting initial output off
print("CUT_TO_HERE");

// Main output
// "toArray()" method allows to get all records
printjson( db.getCollection('jobs').find().toArray() );

Sending the query from terminal

-z key of sed allows treat output as a single multi-line string

$> mongo localhost --quiet myScriptFile.js | sed -z 's/^.*CUT_TO_HERE\n//' > output.json


J
James Yang

In the new mongodb shell 5.0+ mongosh, it integrate the Node.js fs module, so you can simply do below in the new mongosh shell:

fs.writeFileSync('output.json', JSON.stringify(db.collectionName.findOne()))

This also avoid problems such as the ObjectId(...) being included in the tojson result, which is not valid JSON string.

The above code works according to the docs describes:

The MongoDB Shell, mongosh, is a fully functional JavaScript and Node.js 14.x REPL environment for interacting with MongoDB deployments. You can use the MongoDB Shell to test queries and operations directly with your database.

The old mongo shell already marked as Legacy, so use the mongosh if possible.


I needed to convert the iterated response via .toArray(), without it there was an error "Converting circular structure to JSON"