Mongo Shell从文件执行查询并显示结果 [英] Mongo shell execute query from file and show result
本文介绍了Mongo Shell从文件执行查询并显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用mongo shell执行外部文件并在控制台中查看结果?
How to execute external file using mongo shell and see the result in console?
我有外部文件,例如query.js
,我想执行该文件并在cmd中查看结果.
I have external file, like query.js
and I would like to execute it and see the result in cmd.
比方说,文件的内容是:
Let's say, content of the file is:
db.users.find()
推荐答案
将其放入您的query.js
文件:
function get_results (result) {
print(tojson(result));
}
db.col.find().forEach(get_results)
并运行:
mongo db_name query.js
这是一个很好的解释为什么您应该采用这种方式.
Here's a good explanation why you should do it this way.
这篇关于Mongo Shell从文件执行查询并显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文