mongodb-匯出資料到csv檔案或json檔案

來源:互聯網
上載者:User

標籤:mongod   enable   log   val   query   lca   port   ram   pac   

在mongodb的bin目錄下, 有一個mongoexport, 可用於資料的匯出

[[email protected] bin]$ ./mongoexport --helpUsage:  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 exitverbosity 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 outputconnection 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 libraryauthentication 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 usenamespace options:  -d, --db=<database-name>                        database to use  -c, --collection=<collection-name>              collection to useoutput 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 linequerying 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 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}‘      --assertExists                              if specified, export fails if the collection does not exist (default:                                                  false)

 

匯出檔案到json中: 

匯出到json:$ mongoexport.exe  -d TestDB -c TestCollection -o  ./test.json

 

匯出檔案到csv中

./mongoexport --csv -f _id,userId,report_point,matched_line,mode,match_point,predict_point,predict_lines,poi_ids,is_moved,time,date_time -d mapmatch -c 2017-07-04 -o /opt/wenbronk_soft/2017-07-05.csv

可能會出現亂碼, 用記事本另存utf-8 就可以了

 

在使用-q 查詢條件過濾資料的時候,注意兩邊的 ’ (單引號)

當資料量很大時候,可以使用--skip 配合 --limit,限制單個檔案的資料量,方便普通使用者查看

 

mongodb-匯出資料到csv檔案或json檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.