温馨提示×

mongodb怎么导出脚本

小晨
242
2021-07-08 15:00:37
栏目: 云计算

mongodb导出脚本的方法:在mongodb中能够使用mongoexport工具将一个集合导出成JSON格式或CSV格式的文件,语法格式:“mongoexport -d 数据库 -c 集合名称 -o 文件路径”,该语句的参数说明:-d指明数据库的名字、-c指明collection的名字、-o:指明到要导出的文件名。

mongodb怎么导出脚本

具体内容如下:

Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。mongoexport具体用法如下所示:

[root@lb-vm5 bin]# ./mongoexport --help

Usage:  mongoexport <options>

Export data from MongoDB in CSV or JSON format.

See http://docs.mongodb.org/manual/reference/program/mongoexport/ for more information.

general options:

      --help                                      print usage

      --version                                   print the tool version and exit

verbosity options:

      -v, --verbose=<level>                           more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N)

      --quiet                                     hide all log output

connection options:

      -h, --host=<hostname>                           mongodb host to connect to (setname/host1,host2 for replica sets)

      --port=<port>                               server port (can also use --host hostname:port)

ssl options:

      --ssl                                       connect to a mongod or mongos that has ssl enabled

      --sslCAFile=<filename>                      the .pem file containing the root certificate chain from the certificate authority

      --sslPEMKeyFile=<filename>                  the .pem file containing the certificate and key

      --sslPEMKeyPassword=<password>              the password to decrypt the sslPEMKeyFile, if necessary

      --sslCRLFile=<filename>                     the .pem file containing the certificate revocation list

      --sslAllowInvalidCertificates               bypass the validation for server certificates

      --sslAllowInvalidHostnames                  bypass the validation for server name

      --sslFIPSMode                               use FIPS mode of the installed openssl library

authentication options:

      -u, --username=<username>                       username for authentication

      -p, --password=<password>                       password for authentication

      --authenticationDatabase=<database-name>    database that holds the user's credentials

      --authenticationMechanism=<mechanism>       authentication mechanism to use

namespace options:

      -d, --db=<database-name>                        database to use

      -c, --collection=<collection-name>              collection to use

output options:

      -f, --fields=<field>[,<field>]*                 comma separated list of field names (required for exporting CSV) e.g. -f "name,age"

      --fieldFile=<filename>                      file with field names - 1 per line

      --type=<type>                               the output format, either json or csv (defaults to 'json')

      -o, --out=<filename>                            output file; if not specified, stdout is used

      --jsonArray                                 output to a JSON array rather than one object per line

      --pretty                                    output JSON formatted to be human-readable

querying options:

      -q, --query=<json>                              query filter, as a JSON string, e.g., '{x:{$gt:1}}'

      --queryFile=<filename>                      path to a file containing a query filter (JSON)

  -k, --slaveOk                                   allow secondary reads if available (default true)

      --readPreference=<string>|<json>            specify either a preference name or a preference json object

      --forceTableScan                            force a table scan (do not use $snapshot)

      --skip=<count>                              number of documents to skip

      --limit=<count>                             limit the number of documents to export

      --sort=<json>                               sort order, as a JSON string, e.g. '{x:1}'

1.直接导出数据到文件中

./mongoexport -d user_center -c passport -u dev -p 123456-o passport.json

//摩拜单车mongodb查询语句导出 以201612开头 时间小于1482170579000 状态为RENTAL

./bin/mongoexport -d 数据库 -c 集合 -u 用户名 -p 密码 -q  '{bikeRentalStatus:"RENTAL",times:{$regex:/201612.*/i},systimes:{ $lt: NumberLong(1482170579000)}}'  -o 1.jaon

mongodb怎么导出脚本

./bin/mongoexport -d user_center -c passport -u dev -p ***** -q  '{_id:{$in:[171]}}'  -o 1.jaon

参数说明:

-h:指明数据库宿主机的IP

-u:指明数据库的用户名

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-f:指明要导出那些列

-o:指明到要导出的文件名

-q:指明导出数据的过滤条件

-d:指明使用的库

-c:指明要导出的集合

-o:指明要导出的文件名

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

./mongoexport -d user_center -c user_name -u dev -p 123456 --csv -o user_name.json

0