一:数据导出 mongoexport
假设库里有一张 user 表,里面有 2 条记录

> use my_mongodb
switched to db my_mongodb
> db.user.find();
{ "_id" : ObjectId("4f81a4a1779282ca68fd8a5a"), "uid" : 2, "username" : "Jerry", "age" : 100 }
{ "_id" : ObjectId("4f844d1847d25a9ce5f120c4"), "uid" : 1, "username" : "Tom", "age" : 25 }
>

1.导出  JSON 格式的数据

[root@localhost bin]# ./mongoexport -d my_mongodb -c user -o user.dat

connected to: 127.0.0.1
exported 2 records
[root@localhost bin]# cat user.dat
{ "_id" : { "$oid" : "4f81a4a1779282ca68fd8a5a" }, "uid" : 2, "username" : "Jerry", "age" : 100 }
{ "_id" : { "$oid" : "4f844d1847d25a9ce5f120c4" }, "uid" : 1, "username" : "Tom", "age" : 25 }
[root@localhost bin]#

参数说明:
 -d 指明使用的库, 本例中为” my_mongodb”
-c 指明要导出的表, 本例中为”user”
-o 指明要导出的文件名, 本例中为”user.dat”

2. 导出 CSV 格式的文件

[root@localhost bin]# ./mongoexport -d my_mongodb -c user --csv -f uid,username,age -o
user_csv.dat
connected to: 127.0.0.1
exported 2 records
[root@localhost bin]# cat user_csv.dat
uid,username,age
2,"Jerry",100
1,"Tom",25
[root@localhost bin]#

参数说明:
 -csv 指要要导出为csv 格式
 -f 指明需要导出哪些例

更详细的用法可以 mongoexport –help 来查看

二:数据导入 mongoimport  
1. 导入 JSON  数据
先将表user 删除掉,以便演示效果

> db.user.drop();
true
> show collections;
system.indexes
>

导入数据:

[root@localhost bin]# ./mongoimport -d my_mongodb -c user user.dat
connected to: 127.0.0.1
imported 2 objects
[root@localhost bin]#

2.导入  CSV 数据:

[root@localhost bin]# ./mongoimport -d my_mongodb -c user --type csv --headerline --file
user_csv.dat
connected to: 127.0.0.1
imported 3 objects
[root@localhost bin]#

参数说明:
 -type 指明要导入的文件格式
 -headerline 批明不导入第一行,因为第一行是列名
 -file 指明要导入的文件路径

注意:
CSV 格式良好,主流数据库都支持导出为CSV  的格式,所以这种格式非常利于异构数据迁移
Tags:
Mongodb | 评论(0) | 引用(0) | 阅读(4918)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]