mongodb Mongo shell 从文件执行查询并显示结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16326241/
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
Mongo shell execute query from file and show result
提问by Andrei
How to execute external file using mongo shell and see the result in console?
如何使用 mongo shell 执行外部文件并在控制台中查看结果?
I have external file, like query.js
and I would like to execute it and see the result in cmd.
我有外部文件,例如query.js
,我想执行它并在 cmd 中查看结果。
Let's say, content of the file is:
假设文件的内容是:
db.users.find()
回答by alecxe
Put this into your query.js
file:
将其放入您的query.js
文件中:
function get_results (result) {
print(tojson(result));
}
db.col.find().forEach(get_results)
and run:
并运行:
mongo db_name query.js
Here's a good explanationwhy you should do it this way.
这是一个很好的解释,为什么你应该这样做。
回答by Nelu
The simplest way I found of running mongodb queries from a file and seeing the output in the console is this:
我发现从文件运行 mongodb 查询并在控制台中查看输出的最简单方法是:
query.js
:
query.js
:
use my_db;
db.my_collection.findOne()
On the command line:
mongo <query.js
在命令行上:
mongo <query.js
This displays all the output to the console, as if you were running the queries in the mongo shell individually.
这会将所有输出显示到控制台,就像您在 mongo shell 中单独运行查询一样。
回答by Steve Tarver
Good information here - wanted to point out that mongo provides a printjson() function so there is no need to write your own unless you need more functionality than printjson() provides.
这里有很好的信息 - 想指出 mongo 提供了一个 printjson() 函数,因此除非您需要比 printjson() 提供的功能更多的功能,否则无需编写自己的函数。
Example Mongo file (test.js)
示例 Mongo 文件 (test.js)
// Pretty print all documents in test.scratch
use test
db.scratch.find().forEach(printjson)
Command
命令
mongo < test.js
If you want to omit use test
from the mongo file, perhaps to remove IDE error indications for js files, you can specify the target db on the command line:
如果你想use test
从 mongo 文件中省略,也许是为了删除 js 文件的 IDE 错误指示,你可以在命令行中指定目标数据库:
mongo test < test.js
Interesting to note: the above examples use a redirect to push the file into the mongo shell. This calling convention allows you to enter commands just as you would in the shell; including mongo shell convenience commands like use test
.
有趣的是:上面的示例使用重定向将文件推送到 mongo shell。这种调用约定允许您像在 shell 中一样输入命令;包括 mongo shell 方便命令,如use test
.
Mongo provides another script calling convention: mongo test test.js
which omits the redirect operator. This calling convention requires test.js
to be proper javascript and cannot use the mongo shell convenience methods like use test
; one would use the javascript equivalents like getSiblingDB()
.
Mongo 提供了另一个脚本调用约定:mongo test test.js
它省略了重定向操作符。此调用约定需要test.js
使用正确的 javascript,并且不能使用 mongo shell 方便方法,例如use test
;人们会使用 javascript 等价物,如getSiblingDB()
.
回答by laike9m
One thing other answers didn't mention is that use db
command won't work in external script. The best way is to use getSiblingDB
, for example, if I want to use database called my_db
:
其他答案没有提到的一件事是该use db
命令在外部脚本中不起作用。最好的方法是使用getSiblingDB
,例如,如果我想使用名为 的数据库my_db
:
db = db.getSiblingDB("my_db");
function get_results (result) {
print(tojson(result));
}
db.col.find().forEach(get_results)
Then everything works as you would expect. See Write Scripts for the mongo Shell.
然后一切都如您所愿。请参阅为 mongo Shell 编写脚本。
回答by nover
This seems to have changed at some point in the mongo cli, I had to execute the following command to get it to run a file against the database (with mongo cli version 3.4.9)
这似乎在 mongo cli 的某个时候发生了变化,我必须执行以下命令才能让它针对数据库运行文件(使用 mongo cli 版本 3.4.9)
mongo mongodb://192.168.1.1/YourDataBase scriptFile.js
Then replace 192.168.1.1
with the ip / hostname of your db, YourDataBase
with the database name and point to an existing file
然后用192.168.1.1
你的数据库的 ip/hostname替换YourDataBase
为数据库名称并指向现有文件