MongoDB 数据集合导出 与 导入

导出(mongoexport)

导出数据命令:mongoexport -h dbhost -d dbname -c collectionName -o output

-h :数据库地址,MongoDB 服务器所在的 IP 与 端口,如 localhost:27017

-d :指明使用的数据库实例,如 test

-c 指明要导出的集合,如 c1
-p 数据库密码
-u 数据库账号
-o 指明要导出的文件名,如 E:/wmx/mongoDump/c1.json,注意是文件而不是目录,目录不存在时会一同新建

与 《 MongoDB 数据备份 与 恢复》同理,它同样使用安装的 MongoDB 目录下的 bin 目录下的 mongoexport.exe 与 mongoimport.exe

如果想要查看所有的参数信息,可以使用 mongoexport --help 进行查看
C:\Users\Administrator.SC-201707281232>mongoexport --help
Usage:
mongoexport

Export data from MongoDB in CSV or JSON format.

See http://docs.mongodb.org/manual/reference/program/mongoexport/ for more information.

general options:
/help print usage
/version print the tool version and
exit

verbosity options:
/v, /verbose: more detailed log output

操作时,同样不用登陆 MongoDB,在 cmd 命令行中直接操作即可,如下所示,mongoDB 一共 5个 有数据的 库,以 mydb1 数据库实例中的 c1 集合为例进行导出。

show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mydb1 0.000GB
mydb2 0.186GB
db
mydb1
show collections
c1
db.c1.find().count()
100
db.c1.find()
{ “_id” : ObjectId(“5b98bc379253fbe383c9f04e”), “name” : “zhangSan1”, “age” : 1 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f04f”), “name” : “zhangSan2”, “age” : 2 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f050”), “name” : “zhangSan3”, “age” : 3 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f051”), “name” : “zhangSan4”, “age” : 4 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f052”), “name” : “zhangSan5”, “age” : 5 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f053”), “name” : “zhangSan6”, “age” : 6 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f054”), “name” : “zhangSan7”, “age” : 7 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f055”), “name” : “zhangSan8”, “age” : 8 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f056”), “name” : “zhangSan9”, “age” : 9 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f057”), “name” : “zhangSan10”, “age” : 10 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f058”), “name” : “zhangSan11”, “age” : 11 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f059”), “name” : “zhangSan12”, “age” : 12 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f05a”), “name” : “zhangSan13”, “age” : 13 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f05b”), “name” : “zhangSan14”, “age” : 14 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f05c”), “name” : “zhangSan15”, “age” : 15 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f05d”), “name” : “zhangSan16”, “age” : 16 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f05e”), “name” : “zhangSan17”, “age” : 17 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f05f”), “name” : “zhangSan18”, “age” : 18 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f060”), “name” : “zhangSan19”, “age” : 19 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f061”), “name” : “zhangSan20”, “age” : 20 }
Type “it” for more

如下所示,mongoexport -h localhost:27017 -d mydb1 -c c1 -o E:/wmx/mongoDump/c1.json 将 数据库 mydb1 下的 集合 c1 导出到 E:/wmx/mongoDump/c1.json 文件中,存储文件可以是多种形式,如 txt、xls、docs 等等

C:\Users\Administrator.SC-201707281232>mongoexport -h localhost:27017 -d mydb1 -c c1 -o E:/wmx/mongoDump/c1.json
2018-09-12T16:42:07.297+0800 connected to: localhost:27017
2018-09-12T16:42:07.379+0800 exported 100 records

C:\Users\Administrator.SC-201707281232>mongoexport -h localhost:27017 -d mydb1 -c c1 -o E:/wmx/mongoDump/c1.txt
2018-09-12T16:42:58.225+0800 connected to: localhost:27017
2018-09-12T16:42:58.311+0800 exported 100 records

C:\Users\Administrator.SC-201707281232>
如下所示,导出数据成功。

导入(mongoimport)

导入数据命令:mongoimport -h dbhost -d dbname -c collectionname 文件的地址…

-h : 数据库地址,MongoDB 服务器所在的 IP 与 端口,如 localhost:27017

-d :指明使用的库,指明使用的数据库实例,如 test

-c :指明要导入的集合,如 c1、c2、可以和导出时不一致,自定义即可,不存在时会直接创建。
-u 数据库账号
-p 数据库密码
–drop 删除原集合的数据
本地的文件地址:事先导出好的 mongoDB 集合文件

如下所示,先删除 mydb1 库下面的 集合 c1,然后再将本地之前导出好的进行导入恢复

db
mydb1
show tables
c1
db.c1.drop()
true
show tables

如下所示,直接从 cmd 命令行中进行操作,不用登录 MongoDB,将上面备份好的 c1.txt 与 c1.json 文件进行导入,分别导入到数据库 mydb1 下面的 c1 集合 与 c2 集合,c1、c2 集合事先是不存在的。

C:\Users\Administrator.SC-201707281232>mongoimport -h localhost:27017 -d mydb1 -c c2 E:/wmx/mongoDump/c1.txt
2018-09-12T16:56:21.426+0800 connected to: localhost:27017
2018-09-12T16:56:21.752+0800 imported 100 documents

C:\Users\Administrator.SC-201707281232>mongoimport -h localhost:27017 -d mydb1 -c c1 E:/wmx/mongoDump/c1.json
2018-09-12T16:57:08.308+0800 connected to: localhost:27017
2018-09-12T16:57:08.653+0800 imported 100 documents

C:\Users\Administrator.SC-201707281232>
然后登录 MongoDB 再次查询时,数据导入成功

show tables
c1
c2
db.c1.find().count()
100
db.c2.find().count()
100
db.c1.find()
{ “_id” : ObjectId(“5b98bc379253fbe383c9f051”), “name” : “zhangSan4”, “age” : 4 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f04f”), “name” : “zhangSan2”, “age” : 2 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f04e”), “name” : “zhangSan1”, “age” : 1 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f050”), “name” : “zhangSan3”, “age” : 3 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f053”), “name” : “zhangSan6”, “age” : 6 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f052”), “name” : “zhangSan5”, “age” : 5 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f055”), “name” : “zhangSan8”, “age” : 8 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f054”), “name” : “zhangSan7”, “age” : 7 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f056”), “name” : “zhangSan9”, “age” : 9 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f057”), “name” : “zhangSan10”, “age” : 10 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f058”), “name” : “zhangSan11”, “age” : 11 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f05b”), “name” : “zhangSan14”, “age” : 14 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f05a”), “name” : “zhangSan13”, “age” : 13 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f05c”), “name” : “zhangSan15”, “age” : 15 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f05d”), “name” : “zhangSan16”, “age” : 16 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f05e”), “name” : “zhangSan17”, “age” : 17 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f05f”), “name” : “zhangSan18”, “age” : 18 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f059”), “name” : “zhangSan12”, “age” : 12 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f063”), “name” : “zhangSan22”, “age” : 22 }
{ “_id” : ObjectId(“5b98bc379253fbe383c9f060”), “name” : “zhangSan19”, “age” : 19 }
Type “it” for more

原文链接:https://blog.csdn.net/wangmx1993328/article/details/82663617

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值