如何将整个MongoDB数据库转储为text/json? [英] How do I dump an entire MongoDB database as text/json?
问题描述
如何将整个MongoDB数据库转储为文本(纯文本,json或CSV)?
How can I dump an entire MongoDB database as text (plain text, json, or CSV)?
我正在使用不太熟悉的应用程序.我想
I'm using an application I'm not too familiar with. I'd like to
- 清除数据库
- 加载种子数据
- 将整个数据库转储为文本
- 做点事
- 再次转储
然后区别两个!
推荐答案
您可以使用mongoexport
实用程序.默认情况下,它将转储json,但您可以指定转出csv格式.
You can use mongoexport
utility. It will dump out json by default but you can specify to dump out csv format.
mongoexport --help
将为您提供所需的所有选项.
mongoexport --help
will give you all the options you will need.
您提到要从数据库中转储所有集合,并且mongoexport
希望您指定数据库和集合名称,因为它希望转储一个集合.
You mentioned that you would like to dump all the collections from a database, and mongoexport
expects you to specify a database and collection name, since it expects to dump one collection.
您可以编写一个简短的Shell脚本(如果在Windows上,请转换为批处理脚本),以达到以下效果:
You can write a short shell script (if you are on Windows, convert to batch script) to the effect of:
#!/bin/sh
# assuming mongo bin is in your path
host=YOURMONGOHOST
port=YOURMONGOPORT
db=DBYOUWANTTOEXPORT
for c in `mongo --quiet $host:$port/$db --eval 'db.getCollectionNames()' | sed 's/,/ /g'`
do
mongoexport --host $host --port $port -d $db -c $c > $c.json
done
这篇关于如何将整个MongoDB数据库转储为text/json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!