mongodb导出指定条件数据
<p>一、Mongodb导出工具mongoexport</p><p>Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式(类似于表格的形式)的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。<br>mongoexport具体用法</p>
<p>$mongoexport --help</p>
<p>general options:<br> --help print usage<br> --version print the tool version and exit</p>
<p>verbosity options:<br>-v, --verbose more detailed log output (include multiple times for more verbosity, e.g. -vvvvv)<br> --quiet hide all log output</p>
<p>connection options:<br>-h, --host= mongodb host to connect to (setname/host1,host2 for replica sets)<br> --port= server port (can also use --host hostname:port)</p>
<p>ssl options:<br> --ssl connect to a mongod or mongos that has ssl enabled<br> --sslCAFile= the .pem file containing the root certificate chain from the certificate authority<br> --sslPEMKeyFile= the .pem file containing the certificate and key<br> --sslPEMKeyPassword= the password to decrypt the sslPEMKeyFile, if necessary<br> --sslCRLFile= the .pem file containing the certificate revocation list<br> --sslAllowInvalidCertificatesbypass the validation for server certificates<br> --sslAllowInvalidHostnames bypass the validation for server name<br> --sslFIPSMode use FIPS mode of the installed openssl library</p>
<p>authentication options:<br>-u, --username= username for authentication<br>-p, --password= password for authentication<br> --authenticationDatabase= database that holds the user's credentials<br> --authenticationMechanism= authentication mechanism to use</p>
<p>namespace options:<br>-d, --db= database to use<br>-c, --collection= collection to use</p>
<p>output options:<br>-f, --fields= comma separated list of field names (required for exporting CSV) e.g. -f "name,age"<br> --fieldFile= file with field names - 1 per line<br> --type= the output format, either json or csv (defaults to 'json')<br>-o, --out= output file; if not specified, stdout is used<br> --jsonArray output to a JSON array rather than one object per line<br> --pretty output JSON formatted to be human-readable</p>
<p>querying options:<br>-q, --query= query filter, as a JSON string, e.g., '{x:{$gt:1}}'<br>-k, --slaveOk allow secondary reads if available (default true)<br> --forceTableScan force a table scan (do not use $snapshot)<br> --skip= number of documents to skip<br> --limit= limit the number of documents to export<br> --sort= sort order, as a JSON string, e.g. '{x:1}'</p>
<p> </p>
<p>举例:</p>
<p>导出test_db数据库,test_collection集合,uploadDate(日期型)为2019-12-02的数据,导出字段有uploadDate,length,filename,导出文件格式为csv格式</p>
<p>./mongoexport -h 172.128.111.56 --port 27017 -u admin -p admin -d test_db -c test_collection --type=csv -f uploadDate,length,filename -q '{"uploadDate":{"$lte":new Date("2019-12-02T16:00:00.000Z"),"$gt":new Date("2019-12-01T16:00:00.000Z")}}' -o /home/test/mongodb/test_20191202.csv</p>
<p> </p>
<p>有时如果数据需要认证权限,可能会提示如下错误信息:</p>
<p>Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.</p>
<p>原由是由于没有指定用户的认证db,可以通过--authenticationDatabase=database来指定</p>
<p>如上例可以改为</p>
<p>./mongoexport -h 172.128.111.56 --port 27017 -u admin -p admin --authenticationDatabase=admin -d test_db -c test_collection --type=csv -f uploadDate,length,filename -q '{"uploadDate":{"$lte":new Date("2019-12-02T16:00:00.000Z"),"$gt":new Date("2019-12-01T16:00:00.000Z")}}' -o /home/test/mongodb/test_20191202.csv</p><br><br>
来源:https://www.cnblogs.com/ncyhl/p/11985449.html
頁:
[1]