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

具体内容如下:
Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。mongoexport具体用法如下所示:
[root@lb-vm5 bin]# ./mongoexport --helpUsage: mongoexport
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=
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=
mongodb host to connect to (setname/host1,host2 for replica sets) --port=
server port (can also use --host hostname:port) ssl options:
--ssl connect to a mongod or mongos that has ssl enabled
--sslCAFile=
the .pem file containing the root certificate chain from the certificate authority --sslPEMKeyFile=
the .pem file containing the certificate and key --sslPEMKeyPassword=
the password to decrypt the sslPEMKeyFile, if necessary --sslCRLFile=
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 for authentication -p, --password=
password for authentication --authenticationDatabase=
database that holds the user's credentials --authenticationMechanism=
authentication mechanism to use namespace options:
-d, --db=
database to use -c, --collection=
collection to use output options:
-f, --fields=
[, ]* comma separated list of field names (required for exporting CSV) e.g. -f "name,age" --fieldFile=
file with field names - 1 per line --type=
the output format, either json or csv (defaults to 'json') -o, --out=
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=
query filter, as a JSON string, e.g., '{x:{$gt:1}}' --queryFile=
path to a file containing a query filter (JSON) -k, --slaveOk allow secondary reads if available (default true)
--readPreference=
| specify either a preference name or a preference json object --forceTableScan force a table scan (do not use $snapshot)
--skip=
number of documents to skip --limit=
limit the number of documents to export --sort=
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

./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