mongodb导出csv文件

it2025-12-15  11

Mongodb导出csv数据 关于mongodb的客户端,最开始我使用的工具是mongobooster,后来升级变成了nosqlbooster,都是可以直接选择字段进行导出的,步骤如下:

但是后来导出的时候提示要进行收费了,不过依然提供了导出的命令,直接在命令行窗口执行也是可以的,步骤类似如下:

不过后来

我加上一些查询条件的时候发现每次执行导出的数据都是空,类似于以下的输出:

java0904@weigongdeMacBook-Pro ~ % mongoexport -v --host localhost:3717 --username root --password root --authenticationDatabase admin -d "car" -c "vinParse_query" --out /Users/java0904/Downloads/vinParse_query.csv --fields supplier.classname,params.vin,params.key,ip,addtime,timestamp --type csv --query "{\"addtime\":{\"$gt\":\"2020-01-01\",\"$lt\":\"2020-02-02\"}}" --limit 10000000000 --sort {"_id":1} 2020-09-29T14:45:31.255+0800 connected to: mongodb://localhost:3717/ 2020-09-29T14:45:32.275+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:33.277+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:34.277+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:35.276+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:36.277+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:37.276+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:38.278+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:39.279+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:40.279+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:41.278+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:42.277+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:43.279+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:44.279+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:45.276+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:46.279+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:47.279+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:48.279+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:49.279+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:50.279+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:51.278+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:52.279+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:53.278+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:54.279+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:55.279+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:55.447+0800 [........................] car.vinParse_query 0/10000000000 (0.0%) 2020-09-29T14:45:55.447+0800 exported 0 records

你说恼人不,试了很多办法,网上查了很多命令,都不行,最终我查看了一下:

java0904@weigongdeMacBook-Pro ~ % 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 kerberos options: --gssapiServiceName=<service-name> service name to use when authenticating using GSSAPI/Kerberos ('mongodb' by default) --gssapiHostName=<host-name> hostname to use when authenticating using GSSAPI/Kerberos (remote server's address by default) namespace options: -d, --db=<database-name> database to use -c, --collection=<collection-name> collection to use uri options: --uri=mongodb-uri mongodb uri connection string 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') (default: 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 --noHeaderLine export CSV data without a list of field names at the first line --jsonFormat=<type> the extended JSON format to output, either canonical or relaxed (defaults to 'relaxed') (default: relaxed) 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) (default: false) --readPreference=<string>|<json> specify either a preference mode (e.g. 'nearest') or a preference json object (e.g. '{mode: "nearest", tagSets: [{a: "b"}], maxStalenessSeconds: 123}') --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}' --assertExists if specified, export fails if the collection does not exist (default: false)

最有用的是下面这行:

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

你看,他这里写的是用json,我上面写的也是json啊,那么好,我再按照文档的提示操作一下,把字段名addtime去掉双引号,$gt也去掉双引号,外面使用单引号,但是依旧执行不成功,最终,成功的是如下的命令:

java0904@weigongdeMacBook-Pro ~ % mongoexport -v --host localhost:3717 --username root --password root --authenticationDatabase admin -d "car" -c "vinParse_query" --out /Users/java0904/Downloads/vinParse_query_202005.csv --fields supplier.classname,params.vin,params.key,ip,addtime,timestamp,content.code,content.message --type csv --query '{"addtime":{"$gt":"2020-05-01","$lt":"2020-06-01"}}' --sort {"_id":1} 2020-09-29T15:37:41.967+0800 connected to: mongodb://localhost:3717/ 2020-09-29T15:37:43.069+0800 car.vinParse_query 0 2020-09-29T15:37:59.070+0800 car.vinParse_query 0 2020-09-29T15:38:00.072+0800 car.vinParse_query 0 2020-09-29T15:38:01.072+0800 car.vinParse_query 0 2020-09-29T15:38:02.072+0800 car.vinParse_query 0 2020-09-29T15:38:03.069+0800 car.vinParse_query 0 2020-09-29T15:38:04.072+0800 car.vinParse_query 0 2020-09-29T15:38:23.069+0800 car.vinParse_query 0 2020-09-29T15:38:24.069+0800 car.vinParse_query 0 2020-09-29T15:38:25.072+0800 car.vinParse_query 0 2020-09-29T15:38:26.072+0800 car.vinParse_query 8000 2020-09-29T15:38:27.071+0800 car.vinParse_query 8000 2020-09-29T15:38:28.072+0800 car.vinParse_query 8000 2020-09-29T15:38:29.070+0800 car.vinParse_query 8000 2020-09-29T15:38:30.072+0800 car.vinParse_query 8000 2020-09-29T15:38:31.072+0800 car.vinParse_query 8000 2020-09-29T15:38:32.072+0800 car.vinParse_query 8000 2020-09-29T15:38:33.071+0800 car.vinParse_query 8000 ```shell 特此记录,以后再也不需要依赖客户端导出了
最新回复(0)