Mongodb数据的导出与导入

最近升级mongodb库的过程中遇到了数据的导入导出问题。查了一下,目前mongodb自带的导入导出工具还是比较好用的,我操作库的时候主要用了下面两个:

1.导入导出json格式,方便解析,可以给其他业务使用

导出:
mongoexport  --host mongodb.host --port 27017 --db test_db --collection test_table --query '{collect_time:{$gte: 1540656000,$lt: 1540742400}}' --out ./res.json
导入:
mongoimport --host mongodb.host --port 27017 --db test_db --collection test_tableB --file ./res.json
注意,该导出导入方式,不会把索引导出,也不会把索引导入,可以理解为纯文本数据的导出导入

2.导出导入bson格式,这种格式是mongodb的存储格式,只方便于mongodb的解析使用,而且会把索引页导出,导入

导出:
mongodump  --host mongodb.host --port 27017 --db test_db --collection test_table --query '{collect_time:{$gte: 1540656000,$lt: 1540742400}}' --out ./
导入:
mongorestore --host mongodb.host --port 27017 --db test_db --collection test_tableB --dir ./res.bson

可以根据自己的具体需求来选择。速度都比较快千万级数据也就几分钟的事。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章