mongoexport 导出文本数据,不能指定分隔符,有时候不方便。 要实现该功能也简单,几行代码搞定。添加了 –fields-terminated-by(-y) 参数指定分隔符,该参数结合csv参数导出指定分隔符的文本。

[root[@ccj](/user/ccj) ~]#mongoexport  -h 172.17.4.91 --port 27017  -u ccj -p 123 -d test -c t9 -csv -f _id,d -y'#'  -o ccj.txt
connected to: 172.17.4.91:27017
exported 1 records
[root[@ccj](/user/ccj) ~]# more ccj.txt 
_id,d
ObjectID(5178e87593a41742e8a74f0f)#23.0


[root[@ccj](/user/ccj) ~]# 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)
  --version                             print the programs 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
  --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                      if dbpath specified, each db is in a 
                                        separate directory
  --journal                             enable journaling
  -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 fields names - 1 per line
  -q [ --query ] arg                    query filter, as a JSON string
  --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)
  -y [ --fields-terminated-by ] arg (=,)
                                        delimiter
0 回复
需要 登录 后方可回复, 如果你还没有账号你可以 注册 一个帐号。