您的位置:首页 > 编程语言 > Go语言

mongo mongoexport 导出数据

2016-10-09 13:27 288 查看
导出的方法如下:
-h  arg     主机
--port arg  端口
-u  arg     用户名
-p  arg     密码
-d  arg     数据库
-c  arg     集合
-f  arg     字段名 逗号隔开
-q  arg     查询条件 json格式
--csv       导出csv格式
-o  arg     导出的文件名


最常用的格式:

mongoexport -u dotcoo -p 123456 -d dotcoo -c user -q {gender:2} -f username,nickname --csv -o girl.csv


--测试表

 [mongo@redhatB ~]$ mongo test

MongoDB shell version: 2.2.1

connecting to: test

> db.test_2.find();

{ "_id" : ObjectId("50a245153b3b9e81c167fa98"), "id" : 1, "name" : "francs" }

{ "_id" : ObjectId("50a245213b3b9e81c167fa99"), "id" : 2, "name" : "fpZhou" }

{ "_id" : ObjectId("50a245213b3b9e81c167fa9a"), "id" : 3, "name" : "tutu" }

{ "_id" : ObjectId("50a245213b3b9e81c167fa9b"), "id" : 4, "name" : "am", "address" : "zhoushan" }

>
       备注:我们计划导出数据库 test 中的集合 test_2 中的文档。

 

一 以 CSV 格式导出
--1.1 以 csv 格式导出集合 test_2

 [mongo@redhatB tf]$ mongoexport -h 127.0.0.1 -d test -c test_2 -f id,name,address --csv -o test_2.csv 

connected to: 127.0.0.1

exported 4 records
   备注:以上导出数据库 test  的集合 test_2,并将数据以 csv 格式导出。

            -h 表示主机IP或主机名; -d 表示数据库名; -c 表示集合名; -f 表示所选集合的字段; 

            -o 表示导出的文件名。

   
--1.2 查看  test_2.csv 内容



       备注:将文件 test_2.csv 下载到本地 windows 机器上打开,如上所示。

       另外,如果以 csv 格式导出,需要指定导出集合的字段,否则会报以下ERROR。

         
--1.3 不指定 -f 参数

 [mongo@redhatB tf]$  mongoexport -h 127.0.0.1 -d test -c test_2 --csv -o test.csv

connected to: 127.0.0.1
assertion: 9998 you need to specify fields    
        
  

二 以 JSON  格式导出
--2.1 以 JSON 格式导出集合 test_2

 [mongo@redhatB tf]$ mongoexport -h 127.0.0.1 -d test -c test_2  -o test_2.json

connected to: 127.0.0.1

exported 4 records
   备注:默认情况下使用 mongoexport 导出的数据文件格式为 JSON。

  

--2.2 查看 test_2.json 数据

 [mongo@redhatB tf]$ cat test_2.json

{ "_id" : { "$oid" : "50a245153b3b9e81c167fa98" }, "id" : 1, "name" : "francs" }

{ "_id" : { "$oid" : "50a245213b3b9e81c167fa99" }, "id" : 2, "name" : "fpZhou" }

{ "_id" : { "$oid" : "50a245213b3b9e81c167fa9a" }, "id" : 3, "name" : "tutu" }

{ "_id" : { "$oid" : "50a245213b3b9e81c167fa9b" }, "id" : 4, "name" : "am", "address" : "zhoushan" }
   

三 参考
http://docs.mongodb.org/manual/reference/mongoexport/
http://docs.mongodb.org/manual/administration/import-export/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mangodb