一、简介
导入和导出可以操作本地的mongodb服务器,也可以操作远程的服务器。所有有如下通用选项:
-h host 主机
-port port 端口
-u username 用户名
-p password 密码
二、mongoexport导出(导出哪个库,哪张表,哪几列,哪几行)
-d 库名
-c 表名
-f field1,field2... 列名
-q 查询条件
-o 导出的文件名
--csv 导出csv格式(可选)
1.导出json,默认导出了_id:
chenfenlideMacBook-Air:mongodb-osx-x86_64-3.6.3 chenfenli$ ./bin/mongoexport -d shop -c test -f sn,name -q '{sn:{$lte:10}}' -o ../test.json
2018-03-28T11:06:46.770+0800 connected to: localhost
2018-03-28T11:06:46.774+0800 exported 10 records
chenfenlideMacBook-Air:mongodb-osx-x86_64-3.6.3 chenfenli$ cd ..
chenfenlideMacBook-Air:mongodb chenfenli$ ls
data log mongodb-osx-x86_64-3.6.3 test.json
chenfenlideMacBook-Air:mongodb chenfenli$ cat test.json
{"_id":{"$oid":"5abb069b98413bf6935b8899"},"sn":1.0,"name":"name1"}
{"_id":{"$oid":"5abb069b98413bf6935b889a"},"sn":2.0,"name":"name2"}
{"_id":{"$oid":"5abb069b98413bf6935b889b"},"sn":3.0,"name":"name3"}
{"_id":{"$oid":"5abb069b98413bf6935b889c"},"sn":4.0,"name":"name4"}
{"_id":{"$oid":"5abb069b98413bf6935b889d"},"sn":5.0,"name":"name5"}
{"_id":{"$oid":"5abb069b98413bf6935b889e"},"sn":6.0,"name":"name6"}
{"_id":{"$oid":"5abb069b98413bf6935b889f"},"sn":7.0,"name":"name7"}
{"_id":{"$oid":"5abb069b98413bf6935b88a0"},"sn":8.0,"name":"name8"}
{"_id":{"$oid":"5abb069b98413bf6935b88a1"},"sn":9.0,"name":"name9"}
{"_id":{"$oid":"5abb069b98413bf6935b88a2"},"sn":10.0,"name":"name10"}
2.导出csv
chenfenlideMacBook-Air:mongodb-osx-x86_64-3.6.3 chenfenli$ ./bin/mongoexport -d shop -c test -f sn,name -q '{sn:{$lte:10}}' --type csv -o ../test.csv
2018-03-28T11:10:40.013+0800 connected to: localhost
2018-03-28T11:10:40.014+0800 exported 10 records
chenfenlideMacBook-Air:mongodb-osx-x86_64-3.6.3 chenfenli$ cd ..
chenfenlideMacBook-Air:mongodb chenfenli$ ls
data mongodb-osx-x86_64-3.6.3 test.json
log test.csv
chenfenlideMacBook-Air:mongodb chenfenli$ cat test.csv
sn,name
1,name1
2,name2
3,name3
4,name4
5,name5
6,name6
7,name7
8,name8
9,name9
10,name10
chenfenlideMacBook-Air:mongodb chenfenli$
三、mongoimport导入(导出哪个库,哪张表)
-d 导入的数据库
-c 导入的表(不存在会自己创建)
-type csv/json(默认)
-file 文件路径
1.导入json
> show collections
test
chenfenlideMacBook-Air:mongodb-osx-x86_64-3.6.3 chenfenli$ ./bin/mongoimport -d shop -c test2 --type json --file ../test.json
2018-03-28T11:18:51.307+0800 connected to: localhost
2018-03-28T11:18:51.443+0800 imported 10 documents
> show collections
test
test2
> db.test2.find()
{ "_id" : ObjectId("5abb069b98413bf6935b8899"), "sn" : 1, "name" : "name1" }
{ "_id" : ObjectId("5abb069b98413bf6935b889a"), "sn" : 2, "name" : "name2" }
{ "_id" : ObjectId("5abb069b98413bf6935b889c"), "sn" : 4, "name" : "name4" }
{ "_id" : ObjectId("5abb069b98413bf6935b889e"), "sn" : 6, "name" : "name6" }
{ "_id" : ObjectId("5abb069b98413bf6935b889b"), "sn" : 3, "name" : "name3" }
{ "_id" : ObjectId("5abb069b98413bf6935b889d"), "sn" : 5, "name" : "name5" }
{ "_id" : ObjectId("5abb069b98413bf6935b88a0"), "sn" : 8, "name" : "name8" }
{ "_id" : ObjectId("5abb069b98413bf6935b88a1"), "sn" : 9, "name" : "name9" }
{ "_id" : ObjectId("5abb069b98413bf6935b889f"), "sn" : 7, "name" : "name7" }
{ "_id" : ObjectId("5abb069b98413bf6935b88a2"), "sn" : 10, "name" : "name10" }
>
2.导入csv(导出时指定sn和name列,那么导入时也必须指定为sn和name列)
chenfenlideMacBook-Air:mongodb-osx-x86_64-3.6.3 chenfenli$ ./bin/mongoimport -d shop -c test3 -f sn,name --type csv --file ../test.csv
2018-03-28T11:23:08.919+0800 connected to: localhost
2018-03-28T11:23:09.020+0800 imported 11 documents
> show collections
test
test2
test3
> db.test3.find()
{ "_id" : ObjectId("5abb0a9c72d5e010dcedf682"), "sn" : 3, "name" : "name3" }
{ "_id" : ObjectId("5abb0a9c72d5e010dcedf683"), "sn" : 4, "name" : "name4" }
{ "_id" : ObjectId("5abb0a9c72d5e010dcedf684"), "sn" : 1, "name" : "name1" }
{ "_id" : ObjectId("5abb0a9c72d5e010dcedf685"), "sn" : 5, "name" : "name5" }
{ "_id" : ObjectId("5abb0a9c72d5e010dcedf686"), "sn" : 6, "name" : "name6" }
{ "_id" : ObjectId("5abb0a9c72d5e010dcedf687"), "sn" : 7, "name" : "name7" }
{ "_id" : ObjectId("5abb0a9c72d5e010dcedf688"), "sn" : 8, "name" : "name8" }
{ "_id" : ObjectId("5abb0a9c72d5e010dcedf689"), "sn" : 2, "name" : "name2" }
{ "_id" : ObjectId("5abb0a9c72d5e010dcedf68a"), "sn" : "sn", "name" : "name" }
{ "_id" : ObjectId("5abb0a9c72d5e010dcedf68b"), "sn" : 9, "name" : "name9" }
{ "_id" : ObjectId("5abb0a9c72d5e010dcedf68c"), "sn" : 10, "name" : "name10" }
>
四、mongodump导出二进制bson结构及其索引信息(默认是导出到mongo下的dump目录)
-d 库名
-c 表名(不指定则导出所有表)
-f field1,field2... 列名 (可选)
1.规律:
1.导出的文件放在以database命名的目录下。
2.每个表导出2个文件,分别是bson结构的数据文件,json的索引信息。
3.如果不是声明表名,导出所有的表。
chenfenlideMacBook-Air:mongodb-osx-x86_64-3.6.3 chenfenli$ ls
GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES bin
chenfenlideMacBook-Air:mongodb-osx-x86_64-3.6.3 chenfenli$ ./bin/mongodump -d shop -c test
2018-03-28T11:38:55.450+0800 writing shop.test to
2018-03-28T11:38:55.455+0800 done dumping shop.test (100 documents)
chenfenlideMacBook-Air:mongodb-osx-x86_64-3.6.3 chenfenli$ ls
GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES bin dump
chenfenlideMacBook-Air:mongodb-osx-x86_64-3.6.3 chenfenli$ cd dump/
chenfenlideMacBook-Air:dump chenfenli$ ls
shop
chenfenlideMacBook-Air:dump chenfenli$ cd shop
chenfenlideMacBook-Air:shop chenfenli$ ls
test.bson test.metadata.json
chenfenlideMacBook-Air:shop chenfenli$
五、mongorestore导入二进制文件
-d 库名
-path 文件路径
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
chenfenlideMacBook-Air:mongodb-osx-x86_64-3.6.3 chenfenli$ ./bin/mongorestore -d shop -path dump/shop
2018-03-28T11:52:18.797+0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2018-03-28T11:52:18.802+0800 building a list of collections to restore from dump/shop dir
2018-03-28T11:52:18.809+0800 reading metadata for shop.test from dump/shop/test.metadata.json
2018-03-28T11:52:18.923+0800 restoring shop.test from dump/shop/test.bson
2018-03-28T11:52:18.936+0800 restoring indexes for collection shop.test from metadata
2018-03-28T11:52:19.089+0800 finished restoring shop.test (100 documents)
2018-03-28T11:52:19.089+0800 done
chenfenlideMacBook-Air:mongodb-osx-x86_64-3.6.3 chenfenli$
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
shop 0.000GB
> use shop
switched to db shop
> show collections
test
>