mongodb 如何将 mongo 命令结果生成为平面文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12823990/
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
How to get mongo command results in to a flat file
提问by QVSJ
How do I export the results of a MongoDB command to a flat file
如何将 MongoDB 命令的结果导出到平面文件
For example, If I am to get db.collectionname.find()
into a flat file.
例如,如果我要db.collectionname.find()
进入平面文件。
I tried db.collectionname.find() >> "test.txt"
doesnt seem to work.
我试过db.collectionname.find() >> "test.txt"
好像不行。
回答by peshkira
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
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
假设您有一个在本地主机上运行的名为“db”的数据库和一个名为“collection”的集合,这会将所有记录导出到名为 test.txt 的文件中
If you have a longer script that you want to execute you can also create a script.js file and just use
如果您要执行更长的脚本,您还可以创建一个 script.js 文件并使用
mongo 127.0.0.1/db script.js >> test.txt
I hope this helps
我希望这有帮助
回答by Bob Kuhar
I know of no way to do that from the mongo shell directly, but you can get mongoexport to execute queries and send the results to a file with the -q and -o options:
我知道无法直接从 mongo shell 执行此操作,但是您可以使用 mongoexport 执行查询并将结果发送到带有 -q 和 -o 选项的文件:
mongoexport -h mongo.dev.priv -d models -c profiles -q '{ $query : { _id : "MRD461000" } }' -o MRD_Series1.json
The above hits queries the profiles collection in the models database grabbing the JSON document for _id = "MRD641000". Works for me.
以上命中查询模型数据库中的配置文件集合,获取 _id = "MRD641000" 的 JSON 文档。对我来说有效。
回答by Piotr Fryga
Use this
用这个
mongo db_name --username user --password password < query1.js >> result.txt
回答by amic
Try this - returns a json file with the data of the query, you can change .json
for .txt
and other.
试试这个 - 返回一个带有查询数据的 json 文件,你可以更改.json
为.txt
和其他。
mongoexport --db products --collection clicks --query '{"createdInt":{$gte:20190101}, "clientId":"123", "country":"ES"}' --out clicks-2019.json
回答by zevij
Having missed the db needing to be the actual db in Peshkira's answer, here is a general syntax for a one liner in shell (assuming no password):
在 Peshkira 的回答中错过了需要成为实际数据库的数据库,这里是 shell 中单行的通用语法(假设没有密码):
mongo <host>:<db name> --eval "var x = <db name>.<collection name>.<query>; while(x.hasNext()) { printjson( x.next() ) }" >> out.txt
I tested it both on my mac and Google cloud Ubuntu 15 with Mongo 3+.
我在我的 mac 和谷歌云 Ubuntu 15 上用 Mongo 3+ 测试了它。
回答by DarwinFernandez
mongoexport --host 127.0.0.1 --port 27017 --username youruser -p yourpass \
-d yourDatabaseName -c collectionName --type csv \
--fields field1,field2 -q '{"field1" : 1495730914381}' \
--out report.csv
回答by Arnab Ghosh
mongoexport --db db_name --collection collection_name --csv --out file_name.csv -f field1,field2, field3
mongoexport --db db_name --collection collection_name --csv --out file_name.csv -f field1,field2,field3