MongoDB 数据集合导出 与 导入

导出(mongoexport) 

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

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

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

-c 指明要导出的集合,如 c1

-o 指明要导出的文件名,如 E:/wmx/mongoDump/c1.json,注意是文件而不是目录,目录不存在时会一同新建

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

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:<level>                            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、可以和导出时不一致,自定义即可,不存在时会直接创建。

本地的文件地址:事先导出好的 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

猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/82663617