日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

mongoexport/mongimport命令详解

發(fā)布時間:2024/9/19 综合教程 38 生活家
生活随笔 收集整理的這篇文章主要介紹了 mongoexport/mongimport命令详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

mongoexpot 命令參考文檔:

https://docs.mongodb.com/v4.2/reference/program/mongoexport/

創(chuàng)建測試數(shù)據(jù)

use czg
for(i=1;i<=100;i++){db.t1.insert({id:i})}
for(i=1;i<=100;i++){db.t2.insert({id:i,name:"ceshi2"})}
for(i=1;i<=100;i++){db.t3.insert({id:i,name:"ceshi3"})}
for(i=1;i<=100;i++){db.a1.insert({id:i,name:"ceshi4"})}
db.t1.createIndex({id:1})
db.t1.createIndex({name:1})
db.t2.createIndex({id:1})
db.t2.createIndex({name:1})

db.createUser({user:'ceshi',pwd:'c123456', roles:[{role:'readWrite', db:'czg'}]})

1、mongoexport --help 命令詳解

mongoexport
general options:
      --help                                                打印幫助信息
      --version                                             打印版本號

verbosity options:
  -v, --verbose=<level>                                     增加備份過程中日志詳細程序,例如 -vvvv 打的日志最多
      --quiet                                               備份過程中不輸出日志

connection options:
  -h, --host=<hostname>                                     連接地址 (setname/host1,host2 for replica sets)
      --port=<port>                                         端口號 可以 --host hostname: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
      --sslFIPSMode                                         use FIPS mode of the installed openssl library
      --tlsInsecure                                         bypass the validation for server's certificate chain and host name

authentication options:
  -u, --username=<username>                                 用戶名
  -p, --password=<password>                                 密碼
      --authenticationDatabase=<database-name>              指定驗證庫
      --authenticationMechanism=<mechanism>                 指定驗證機制

kerberos options:
      --gssapiServiceName=<service-name>                    service name to use when authenticating using GSSAPI/Kerberos (default: mongodb)
      --gssapiHostName=<host-name>                          hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's
                                                            address>)

uri options:
      --uri=mongodb-uri                                     mongodb uri 連接信息

namespace options:
  -d, --db=<database-name>                                  導出庫名稱
  -c, --collection=<collection-name>                        導出集合名稱
          
uri options:          
      --uri=mongodb-uri                                     mongodb uri 連接信息
          
output options:          
  -f, --fields=<field>[,<field>]*                           指定一個或多個列進行導出 -f "name,age"
      --fieldFile=<filename>                                --fields的替代參數(shù),指定多個列到文件中,文件必須以0x0A結(jié)尾,僅對-type=csv時有效
      --type=<type>                                         輸出格式,默認為json格式 (defaults to 'json')
  -o, --out=<filename>                                      輸出文件,如果未指定,輸出到當前屏幕
      --jsonArray                                           導出結(jié)果全部寫入到單個json數(shù)組中,默認情況下,每條記錄是一個json
      --pretty                                              以易讀的格式輸出文檔。
      --noHeaderLine                                        在輸出CSV格式時,第一行都是字段名稱,使用這個參數(shù)后,將不寫入字段名稱
      --jsonFormat=<type>                                   提定輸出模式為規(guī)范模式或?qū)捤赡J剑J為寬松模式
          
querying options:          
  -q, --query=<json>                                        指定查詢條件進行過濾, '{x:{$gt:1}}'
      --queryFile=<filename>                                指定查詢文件進行過濾 (JSON)
  -k, --slaveOk                                             3.2版本后已棄用 (default true)
      --readPreference=<string>|<json>                      讀偏好設置
      --forceTableScan                                      force a table scan (do not use $snapshot or hint _id). Deprecated since this is default
                                                            behavior on WiredTiger
      --skip=<count>                                        控制從哪里開始導出數(shù)據(jù)
      --limit=<count>                                       限制導出條數(shù)
      --sort=<json>                                         導出結(jié)果進行排序,結(jié)果集必須小于32M e.g. '{x:1}'
      --assertExists                                        if specified, export fails if the collection does not exist

示例:

1.1 備份集合,默認為json文件

mongoexport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t2 -o /root/mongo_export/czg_t2.json

1.2 備份集合,存儲為csv 文件,必須要指定導出列

mongoexport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t2 --type=csv --fields=id,name -o /root/mongo_export/czg_t2.csv

1.3 如果列比較多,保存成為一個文件,指定文件中的列進行導出

root@ip-172-31-30-45:~/mongo_export# cat fields.txt
id
name
----------------------分割線----------------------
mongoexport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t2 --type=csv --fieldFile=/root/mongo_export/fields.txt -o /root/mongo_export/czg_t2.csv

1.4 導出的csv文件默認帶字段名稱,使用--noHeaderLine 進行不顯示字段名

mongoexport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t2 --type=csv --fields=id,name --noHeaderLine -o /root/mongo_export/czg_t2.csv

1.5 以uri信息連接數(shù)據(jù)庫,導出數(shù)據(jù)

mongoexport --uri="mongodb://root:c123456@127.0.0.1:27017/czg?authsource=admin" -c t2 -o /root/mongo_export/czg_t2.json

1.6 以過濾條件導出數(shù)據(jù)

mongoexport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t2 -q='{"id":{"$gte":50}}' -o /root/mongo_export/czg_t2.json

1.7 限制導出條數(shù)

mongoexport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t2 -q='{"id":{"$gte":50}}' --limit=10 -o /root/mongo_export/czg_t2.json

2、

1、mongoexport --help 命令詳解

general options:
      --help                                                打印幫助信息
      --version                                             打印版本號

verbosity options:
  -v, --verbose=<level>                                     增加備份過程中日志詳細程序,例如 -vvvv 打的日志最多
      --quiet                                               備份過程中不輸出日志

connection options:
  -h, --host=<hostname>                                     連接地址 (setname/host1,host2 for replica sets)
      --port=<port>                                         端口號 可以 --host hostname: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
      --sslFIPSMode                                         use FIPS mode of the installed openssl library
      --tlsInsecure                                         bypass the validation for server's certificate chain and host name

authentication options:
  -u, --username=<username>                                 用戶名
  -p, --password=<password>                                 密碼
      --authenticationDatabase=<database-name>              指定驗證庫
      --authenticationMechanism=<mechanism>                 指定驗證機制

kerberos options:
      --gssapiServiceName=<service-name>                    service name to use when authenticating using GSSAPI/Kerberos (default: mongodb)
      --gssapiHostName=<host-name>                          hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's
                                                            address>)

uri options:
      --uri=mongodb-uri                                     mongodb uri 連接信息

namespace options:
  -d, --db=<database-name>                                  導出庫名稱
  -c, --collection=<collection-name>                        導出集合名稱
          
uri options:          
      --uri=mongodb-uri                                     mongodb uri 連接信息
          
output options:          
  -f, --fields=<field>[,<field>]*                           指定一個或多個列進行導出 -f "name,age"
      --fieldFile=<filename>                                --fields的替代參數(shù),指定多個列到文件中,文件必須以0x0A結(jié)尾,僅對-type=csv時有效
      --type=<type>                                         輸出格式,默認為json格式 (defaults to 'json')
  -o, --out=<filename>                                      輸出文件,如果未指定,輸出到當前屏幕
      --jsonArray                                           導出結(jié)果全部寫入到單個json數(shù)組中,默認情況下,每條記錄是一個json
      --pretty                                              以易讀的格式輸出文檔。
      --noHeaderLine                                        在輸出CSV格式時,第一行都是字段名稱,使用這個參數(shù)后,將不寫入字段名稱
      --jsonFormat=<type>                                   提定輸出模式為規(guī)范模式或?qū)捤赡J剑J為寬松模式
          
querying options:          
  -q, --query=<json>                                        指定查詢條件進行過濾, '{x:{$gt:1}}'
      --queryFile=<filename>                                指定查詢文件進行過濾 (JSON)
  -k, --slaveOk                                             3.2版本后已棄用 (default true)
      --readPreference=<string>|<json>                      讀偏好設置
      --forceTableScan                                      force a table scan (do not use $snapshot or hint _id). Deprecated since this is default
                                                            behavior on WiredTiger
      --skip=<count>                                        控制從哪里開始導出數(shù)據(jù)
      --limit=<count>                                       限制導出條數(shù)
      --sort=<json>                                         導出結(jié)果進行排序,結(jié)果集必須小于32M e.g. '{x:1}'
      --assertExists                                        if specified, export fails if the collection does not exist

2.1 將 czg_t2.json 文件導入到 czg庫,tt2集合中

mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c tt2 /root/mongo_export/czg_t2.json

2.2 將 czg_t2.json 文件導入到 czg庫,tt2集合中,如果遇到?jīng)_突,則覆蓋寫

mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c tt2 --mode=upsert /root/mongo_export/czg_t2.json

2.3 將czg_t2.csv 文件導入到 czg庫,tt2_csv集合中,導入時必須指定列名稱 (如果備份文件第一行是列名稱,也會被當成數(shù)據(jù)導入到數(shù)據(jù)庫中)

mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c tt2_csv --type=csv --fields=id,name /root/mongo_export/czg_t2.csv

2.4 將czg_t2.csv 文件導入到 czg庫,tt2_csv集合中,使用 --headerline 參數(shù),使用csv文件第一行做為字段名稱

mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c tt2_csv --type=csv --headerline /root/mongo_export/czg_t2.csv

2.5 不帶-c參數(shù)導入數(shù)據(jù),會使用文件名做為集合名,導入后集合名稱為 czg_t2

mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg --type=csv --headerline /root/mongo_export/czg_t2.csv

2.6 導入數(shù)據(jù)時,指定字段類型進行導入 (注意:csv文件第一行不要有字段名稱)

mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c tt2_types --type=csv --columnsHaveTypes --fields="id.int32(),name.string()" /root/mongo_export/czg_t2.csv

2.7 導入數(shù)據(jù)時,忽略掉csv文件中的空白字段

創(chuàng)建一些測試數(shù)據(jù)
root@ip-172-31-30-45:~/mongo_export# cat test.txt
id,name,age
1,,
2,chai,10
3,,20

----------------分割線-------------------
mongoimport -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t_ignore --type=csv --fields=id,name --ignoreBlanks /root/mongo_export/test.txt

總結(jié)

以上是生活随笔為你收集整理的mongoexport/mongimport命令详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。