mongodb 在 mongo shell 中将 Mongo 查询输出打印到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22565231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:38:11  来源:igfitidea点击:

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

mongodbiomongodb-querymongo-shell

提问by Parijat Kalia

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

使用 Mongo 2 天,我有 SQL 背景,所以请耐心等待。与 mysql 一样,在 MySQL 命令行中将查询结果输出到机器上的文件非常方便。我试图了解如何在外壳中对Mongo 做同样的事情

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

通过在 shell 之外并执行以下命令,我可以轻松获得我想要的查询的输出:

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.

上面的方式没问题,但是需要我退出mongo shell或者打开一个新的终端标签来执行这个命令。如果我可以在仍然在外壳内的情况下简单地执行此操作,那将非常方便。

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

PS:这个问题是我在SO 上发布的一个问题的一个分支

回答by Roberto

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

AFAIK,没有输出到文件的交互选项,之前有一个与此相关的问题:将mongodb shell 输出打印到文件

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

但是,如果您使用 tee 命令调用 shell,则可以记录所有 shell 会话:

$ 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:

要删除所有命令并仅保留 json 输出,您可以使用类似于以下内容的命令:

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

Then you'll get:

然后你会得到:

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

回答by Jyotman Singh

We can do it this way -

我们可以这样做 -

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

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

shellBatchSize参数用于确定允许 mongo 客户端打印多少行。其默认值为 20。

回答by 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:

如果您使用脚本文件、数据库地址和 --quiet 参数调用 shell,您可以将输出(例如使用 print() 制作)重定向到一个文件:

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

回答by 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
  • 在 JS 文件中编写 mongo 查询并从终端发送
  • 以编程方式切换/定义数据库
  • 输出所有找到的记录
  • 切割初始输出线
  • 将输出保存到 JSON 文件中

myScriptFile.js

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

从终端发送查询

-zkey of sedallows treat output as a single multi-line string

-zsed允许将输出视为单个多行字符串

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

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

回答by kris

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

简单地增加显示的结果数量可能对您有用

In the mongo shell > DBQuery.shellBatchSize = 3000

在 mongo 外壳中 > 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)

(来自:https: //stackoverflow.com/a/3705615/1290746