如何获取mongo命令生成平面文件 [英] How to get mongo command results in to a flat file
本文介绍了如何获取mongo命令生成平面文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将MongoDB命令的结果导出到平面文件
How do I export the results of a MongoDB command to a flat file
例如,如果我要将db.collectionname.find()
放入平面文件中.
For example, If I am to get db.collectionname.find()
into a flat file.
我尝试了db.collectionname.find() >> "test.txt"
似乎无效.
推荐答案
您可以从命令行尝试以下操作
you can try the following from the command line
mongo 127.0.0.1/db --eval "var c = db.collection.find(); while(c.hasNext()) {printjson(c.next())}" >> test.txt
假设您有一个名为"db"的数据库在本地主机上运行,并且有一个名为"collection"的集合,这会将所有记录导出到一个名为test.txt的文件中
assuming you have a database called 'db' running on localhost and a collection called 'collection' this will export all records into a file called test.txt
如果要执行的脚本较长,还可以创建script.js文件 并使用
If you have a longer script that you want to execute you can also create a script.js file and just use
mongo 127.0.0.1/db script.js >> test.txt
我希望这对您有帮助
这篇关于如何获取mongo命令生成平面文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文