mongoexport和导入工具mongoimport介绍

1.7.1.导出工具mongoexport
Mongodb中的mongoexport工具可以把一个库中的collection导出成JSON格式或CSV格式的文件。可以通过指定的内置参数导出数据项,当然导出的时候可以排序和指定条件。
mongoexport具体用法如下所示:
首先进入mongodb的安装目录bin目录下
Shell代码

[root@iZ25ed9nobgZ bin]# ./mongoexport  --help
Export MongoDB data to CSV, TSV or JSON files.
Options:
  --help                                produce help message
  -v [ --verbose ]                      be more verbose (include multiple times
                                        for more verbosity e.g. -vvvvv)
  --quiet                               silence all non error diagnostic 
                                        messages
  --version                             print the program's version and exit
  -h [ --host ] arg                     mongo host to connect to ( <set 
                                        name>/s1,s2 for sets)
  --port arg                            server port. Can also use --host 
                                        hostname:port
  --ipv6                                enable IPv6 support (disabled by 
                                        default)
  -u [ --username ] arg                 username
  -p [ --password ] arg                 password
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
                                        authentication mechanism
  --gssapiServiceName arg (=mongodb)    Service name to use when authenticating
                                        using GSSAPI/Kerberos
  --gssapiHostName arg                  Remote host name to use for purpose of 
                                        GSSAPI/Kerberos authentication
  --dbpath arg                          directly access mongod database files 
                                        in the given path, instead of 
                                        connecting to a mongod  server - needs 
                                        to lock the data directory, so cannot 
                                        be used if a mongod is currently 
                                        accessing the same path
  --directoryperdb                      each db is in a separate directory 
                                        (relevant only if dbpath specified)
  --journal                             enable journaling (relevant only if 
                                        dbpath specified)
  -d [ --db ] arg                       database to use
  -c [ --collection ] arg               collection to use (some commands)
  -f [ --fields ] arg                   comma separated list of field names 
                                        e.g. -f name,age
  --fieldFile arg                       file with field names - 1 per line
  -q [ --query ] arg                    query filter, as a JSON string, e.g., 
                                        '{x:{$gt:1}}'
  --csv                                 export to csv instead of json
  -o [ --out ] arg                      output file; if not specified, stdout 
                                        is used
  --jsonArray                           output to a json array rather than one 
                                        object per line
  -k [ --slaveOk ] arg (=1)             use secondaries for export if 
                                        available, default true
  --forceTableScan                      force a table scan (do not use 
                                        $snapshot)
  --skip arg (=0)                       documents to skip, default 0
  --limit arg (=0)                      limit the numbers of documents 
                                        returned, default all
  --sort arg                            sort order, as a JSON string, e.g., 
                                        '{x:1}'

参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导出那些列
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
-csv:指明要导出为csv格式
-f:指明需要导出classid、name、age这3列的数据(csv类型必须指定列)

实例:mydb库中存在着一个user集合,集合中数据如下:
Js代码

db.user.find();
{ “_id” : ObjectId(“5625a56a02a19713a37259cc”), “id” : “5” }
{ “_id” : ObjectId(“5625a1d402a19713a37259ca”), “age” : 122, “count” : 100, “scores” : 20, “words” : [ 4, 5, 6 ], “title” : [ “t1” ] }
{ “_id” : ObjectId(“5625baed226e076f3161b848”), “id” : 1, “score” : 6 }
{ “_id” : ObjectId(“5625baed226e076f3161b849”), “id” : null }
{ “_id” : ObjectId(“5625baed226e076f3161b84a”), “id” : 4, “score” : 7 }
{ “_id” : ObjectId(“5625baed226e076f3161b84b”), “id” : 3, “score” : 8 }
{ “_id” : ObjectId(“5625baed226e076f3161b84c”), “id” : 2, “name” : “ksf” }
{ “_id” : ObjectId(“5625d7bb7d1b1fbd265f9c10”), “userid” : “001”, “comment” : [ { “name” : “joe dave”, “size” : 12 }, { “name” : “aa”, “size” : 12 } ] }
{ “_id” : ObjectId(“562616c89f0ba19ca2a584d4”), “userid” : “001” }
{ “_id” : ObjectId(“5627043ddc6a998d44600fd1”), “age” : 10, “words” : [ 4, 5, 1 ] }
{ “_id” : ObjectId(“56270447dc6a998d44600fd2”), “age” : 11, “words” : [ 4, 3, 1 ] }
{ “_id” : ObjectId(“56270450dc6a998d44600fd3”), “age” : 10, “words” : [ 6, 5, 2 ] }

1.直接导出数据到文件中
[root@iZ25ed9nobgZ bin]# ./mongoexport -d mydb -c user -o user.data
connected to: 127.0.0.1
exported 12 records
命令执行完后使用ll命令查看,发现目录下生成了一个user.data的文件
Shell代码
-rw-r–r– 1 root root 1057 Oct 21 16:05 user.data
查看该文件信息,具体信息如下:
Shell代码

more user.data或者 cat user.data
{ "_id" : { "$oid" : "5625a1d402a19713a37259ca" }, "age" : 122, "count" : 100, "scores" : 20, "words" : [ 4, 5, 6 ], "title" : [ "t1" ] }
{ "_id" : { "$oid" : "5625a56a02a19713a37259cc" }, "id" : "5" }
{ "_id" : { "$oid" : "5625baed226e076f3161b848" }, "id" : 1, "score" : 6 }
{ "_id" : { "$oid" : "5625baed226e076f3161b849" }, "id" : null }
{ "_id" : { "$oid" : "5625baed226e076f3161b84a" }, "id" : 4, "score" : 7 }
{ "_id" : { "$oid" : "5625baed226e076f3161b84b" }, "id" : 3, "score" : 8 }
{ "_id" : { "$oid" : "5625baed226e076f3161b84c" }, "id" : 2, "name" : "ksf" }
{ "_id" : { "$oid" : "5625d7bb7d1b1fbd265f9c10" }, "userid" : "001", "comment" : [ { "name" : "joe dave", "size" : 12 }, { "name" : "aa", "size" : 12 } ] }
{ "_id" : { "$oid" : "562616c89f0ba19ca2a584d4" }, "userid" : "001" }
{ "_id" : { "$oid" : "5627043ddc6a998d44600fd1" }, "age" : 10, "words" : [ 4, 5, 1 ] }
{ "_id" : { "$oid" : "56270447dc6a998d44600fd2" }, "age" : 11, "words" : [ 4, 3, 1 ] }
{ "_id" : { "$oid" : "56270450dc6a998d44600fd3" }, "age" : 10, "words" : [ 6, 5, 2 ] }

从上面的结果可以看出,我们在导出数据时没有显示指定导出样式 ,默认导出了JSON格式的数据。如果我们需要导出CSV格式的数据,则需要使用–csv参数,具体如下所示:
Shell代码

[root@iZ25ed9nobgZ bin]# ./mongoexport -d mydb -c user --csv -f _id,id -o  user_csv.data                              
connected to: 127.0.0.1
Invalid BSON object type for CSV output: 10
exported 12 records
[root@iZ25ed9nobgZ bin]# more user_csv.data 
_id,id
ObjectID(5625a1d402a19713a37259ca),
ObjectID(5625a56a02a19713a37259cc),"5"
ObjectID(5625baed226e076f3161b848),1.0
ObjectID(5625baed226e076f3161b849),
ObjectID(5625baed226e076f3161b84a),4.0
ObjectID(5625baed226e076f3161b84b),3.0
ObjectID(5625baed226e076f3161b84c),2.0
ObjectID(5625d7bb7d1b1fbd265f9c10),
ObjectID(562616c89f0ba19ca2a584d4),
ObjectID(5627043ddc6a998d44600fd1),
ObjectID(56270447dc6a998d44600fd2),
ObjectID(56270450dc6a998d44600fd3),

由上面结果可以看出,mongoexport成功地将数据根据csv格式导出到了user_csv.data 文件中。
1.7.2.导入工具mongoimport
Mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。该工具可以导入JSON格式数据,也可以导入CSV格式数据。具体使用如下所示:
Shell代码

[root@iZ25ed9nobgZ bin]# ./mongoimport --help
Import CSV, TSV or JSON data into MongoDB.

When importing JSON documents, each document must be a separate line of the input file.

Example:
  mongoimport --host myhost --db my_cms --collection docs < mydocfile.json

Options:
  --help                                produce help message
  -v [ --verbose ]                      be more verbose (include multiple times
                                        for more verbosity e.g. -vvvvv)
  --quiet                               silence all non error diagnostic 
                                        messages
  --version                             print the program's version and exit
  -h [ --host ] arg                     mongo host to connect to ( <set 
                                        name>/s1,s2 for sets)
  --port arg                            server port. Can also use --host 
                                        hostname:port
  --ipv6                                enable IPv6 support (disabled by 
                                        default)
  -u [ --username ] arg                 username
  -p [ --password ] arg                 password
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
                                        authentication mechanism
  --gssapiServiceName arg (=mongodb)    Service name to use when authenticating
                                        using GSSAPI/Kerberos
  --gssapiHostName arg                  Remote host name to use for purpose of 
                                        GSSAPI/Kerberos authentication
  --dbpath arg                          directly access mongod database files 
                                        in the given path, instead of 
                                        connecting to a mongod  server - needs 
                                        to lock the data directory, so cannot 
                                        be used if a mongod is currently 
                                        accessing the same path
  --directoryperdb                      each db is in a separate directory 
                                        (relevant only if dbpath specified)
  --journal                             enable journaling (relevant only if 
                                        dbpath specified)
  -d [ --db ] arg                       database to use
  -c [ --collection ] arg               collection to use (some commands)
  -f [ --fields ] arg                   comma separated list of field names 
                                        e.g. -f name,age
  --fieldFile arg                       file with field names - 1 per line
  --ignoreBlanks                        if given, empty fields in csv and tsv 
                                        will be ignored
  --type arg                            type of file to import.  default: json 
                                        (json,csv,tsv)
  --file arg                            file to import from; if not specified 
                                        stdin is used
  --drop                                drop collection first 
  --headerline                          first line in input file is a header 
                                        (CSV and TSV only)
  --upsert                              insert or update objects that already 
                                        exist
  --upsertFields arg                    comma-separated fields for the query 
                                        part of the upsert. You should make 
                                        sure this is indexed
  --stopOnError                         stop importing at first error rather 
                                        than continuing
  --jsonArray                           load a json array, not one item per 
                                        line. Currently limited to 16MB.

参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导入那些列
-type:指明要导入的文件格式
-headerline:指明第一行是列名,不需要导入
-file:指明要导入的文件
示例:先删除user中的数据,并验证
Js代码

db.user.remove({});
WriteResult({ “nRemoved” : 12 })
db.user.find();
然后再导入上面导出的user.data文件中的内容
Shell代码

[root@iZ25ed9nobgZ bin]# ./mongoimport -d mydb -c user  user.data    
connected to: 127.0.0.1
2015-10-21T16:24:46.456+0800 check 9 12
2015-10-21T16:24:46.457+0800 imported 12 objects

查询user集合中的数据.
Js代码

> db.user.find();
{ "_id" : ObjectId("5625a1d402a19713a37259ca"), "age" : 122, "count" : 100, "scores" : 20, "words" : [ 4, 5, 6 ], "title" : [ "t1" ] }
{ "_id" : ObjectId("5625a56a02a19713a37259cc"), "id" : "5" }
{ "_id" : ObjectId("5625baed226e076f3161b848"), "id" : 1, "score" : 6 }
{ "_id" : ObjectId("5625baed226e076f3161b849"), "id" : null }
{ "_id" : ObjectId("5625baed226e076f3161b84a"), "id" : 4, "score" : 7 }
{ "_id" : ObjectId("5625baed226e076f3161b84b"), "id" : 3, "score" : 8 }
{ "_id" : ObjectId("5625baed226e076f3161b84c"), "id" : 2, "name" : "ksf" }
{ "_id" : ObjectId("5625d7bb7d1b1fbd265f9c10"), "userid" : "001", "comment" : [ { "name" : "joe dave", "size" : 12 }, { "name" : "aa", "size" : 12 } ] }
{ "_id" : ObjectId("562616c89f0ba19ca2a584d4"), "userid" : "001" }
{ "_id" : ObjectId("5627043ddc6a998d44600fd1"), "age" : 10, "words" : [ 4, 5, 1 ] }
{ "_id" : ObjectId("56270447dc6a998d44600fd2"), "age" : 11, "words" : [ 4, 3, 1 ] }
{ "_id" : ObjectId("56270450dc6a998d44600fd3"), "age" : 10, "words" : [ 6, 5, 2 ] }

证明数据导入成功
上面演示的是导入JSON格式的文件中的内容也是默认的方式,如果要导入CSV格式文件或者csv格式或者tsv中的内容,则需要通过–type参数指定导入格式。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值