mongoexport/mongimport命令详解
mongoexpot 命令參考文檔:
https://docs.mongodb.com/v4.2/reference/program/mongoexport/
創(chuàng)建測(cè)試數(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 打印版本號(hào)
verbosity options:
-v, --verbose=<level> 增加備份過(guò)程中日志詳細(xì)程序,例如 -vvvv 打的日志最多
--quiet 備份過(guò)程中不輸出日志
connection options:
-h, --host=<hostname> 連接地址 (setname/host1,host2 for replica sets)
--port=<port> 端口號(hào) 可以 --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> 指定驗(yàn)證庫(kù)
--authenticationMechanism=<mechanism> 指定驗(yàn)證機(jī)制
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> 導(dǎo)出庫(kù)名稱
-c, --collection=<collection-name> 導(dǎo)出集合名稱
uri options:
--uri=mongodb-uri mongodb uri 連接信息
output options:
-f, --fields=<field>[,<field>]* 指定一個(gè)或多個(gè)列進(jìn)行導(dǎo)出 -f "name,age"
--fieldFile=<filename> --fields的替代參數(shù),指定多個(gè)列到文件中,文件必須以0x0A結(jié)尾,僅對(duì)-type=csv時(shí)有效
--type=<type> 輸出格式,默認(rèn)為json格式 (defaults to 'json')
-o, --out=<filename> 輸出文件,如果未指定,輸出到當(dāng)前屏幕
--jsonArray 導(dǎo)出結(jié)果全部寫入到單個(gè)json數(shù)組中,默認(rèn)情況下,每條記錄是一個(gè)json
--pretty 以易讀的格式輸出文檔。
--noHeaderLine 在輸出CSV格式時(shí),第一行都是字段名稱,使用這個(gè)參數(shù)后,將不寫入字段名稱
--jsonFormat=<type> 提定輸出模式為規(guī)范模式或?qū)捤赡J剑J(rèn)為寬松模式
querying options:
-q, --query=<json> 指定查詢條件進(jìn)行過(guò)濾, '{x:{$gt:1}}'
--queryFile=<filename> 指定查詢文件進(jìn)行過(guò)濾 (JSON)
-k, --slaveOk 3.2版本后已棄用 (default true)
--readPreference=<string>|<json> 讀偏好設(shè)置
--forceTableScan force a table scan (do not use $snapshot or hint _id). Deprecated since this is default
behavior on WiredTiger
--skip=<count> 控制從哪里開始導(dǎo)出數(shù)據(jù)
--limit=<count> 限制導(dǎo)出條數(shù)
--sort=<json> 導(dǎo)出結(jié)果進(jìn)行排序,結(jié)果集必須小于32M e.g. '{x:1}'
--assertExists if specified, export fails if the collection does not exist
示例:
1.1 備份集合,默認(rèn)為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 備份集合,存儲(chǔ)為csv 文件,必須要指定導(dǎo)出列
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 如果列比較多,保存成為一個(gè)文件,指定文件中的列進(jìn)行導(dǎo)出
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 導(dǎo)出的csv文件默認(rèn)帶字段名稱,使用--noHeaderLine 進(jìn)行不顯示字段名
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 以u(píng)ri信息連接數(shù)據(jù)庫(kù),導(dǎo)出數(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 以過(guò)濾條件導(dǎo)出數(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 限制導(dǎo)出條數(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 打印版本號(hào)
verbosity options:
-v, --verbose=<level> 增加備份過(guò)程中日志詳細(xì)程序,例如 -vvvv 打的日志最多
--quiet 備份過(guò)程中不輸出日志
connection options:
-h, --host=<hostname> 連接地址 (setname/host1,host2 for replica sets)
--port=<port> 端口號(hào) 可以 --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> 指定驗(yàn)證庫(kù)
--authenticationMechanism=<mechanism> 指定驗(yàn)證機(jī)制
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> 導(dǎo)出庫(kù)名稱
-c, --collection=<collection-name> 導(dǎo)出集合名稱
uri options:
--uri=mongodb-uri mongodb uri 連接信息
output options:
-f, --fields=<field>[,<field>]* 指定一個(gè)或多個(gè)列進(jìn)行導(dǎo)出 -f "name,age"
--fieldFile=<filename> --fields的替代參數(shù),指定多個(gè)列到文件中,文件必須以0x0A結(jié)尾,僅對(duì)-type=csv時(shí)有效
--type=<type> 輸出格式,默認(rèn)為json格式 (defaults to 'json')
-o, --out=<filename> 輸出文件,如果未指定,輸出到當(dāng)前屏幕
--jsonArray 導(dǎo)出結(jié)果全部寫入到單個(gè)json數(shù)組中,默認(rèn)情況下,每條記錄是一個(gè)json
--pretty 以易讀的格式輸出文檔。
--noHeaderLine 在輸出CSV格式時(shí),第一行都是字段名稱,使用這個(gè)參數(shù)后,將不寫入字段名稱
--jsonFormat=<type> 提定輸出模式為規(guī)范模式或?qū)捤赡J剑J(rèn)為寬松模式
querying options:
-q, --query=<json> 指定查詢條件進(jìn)行過(guò)濾, '{x:{$gt:1}}'
--queryFile=<filename> 指定查詢文件進(jìn)行過(guò)濾 (JSON)
-k, --slaveOk 3.2版本后已棄用 (default true)
--readPreference=<string>|<json> 讀偏好設(shè)置
--forceTableScan force a table scan (do not use $snapshot or hint _id). Deprecated since this is default
behavior on WiredTiger
--skip=<count> 控制從哪里開始導(dǎo)出數(shù)據(jù)
--limit=<count> 限制導(dǎo)出條數(shù)
--sort=<json> 導(dǎo)出結(jié)果進(jìn)行排序,結(jié)果集必須小于32M e.g. '{x:1}'
--assertExists if specified, export fails if the collection does not exist
2.1 將 czg_t2.json 文件導(dǎo)入到 czg庫(kù),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 文件導(dǎo)入到 czg庫(kù),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 文件導(dǎo)入到 czg庫(kù),tt2_csv集合中,導(dǎo)入時(shí)必須指定列名稱 (如果備份文件第一行是列名稱,也會(huì)被當(dāng)成數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中)
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 文件導(dǎo)入到 czg庫(kù),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ù)導(dǎo)入數(shù)據(jù),會(huì)使用文件名做為集合名,導(dǎo)入后集合名稱為 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 導(dǎo)入數(shù)據(jù)時(shí),指定字段類型進(jìn)行導(dǎo)入 (注意: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 導(dǎo)入數(shù)據(jù)時(shí),忽略掉csv文件中的空白字段
創(chuàng)建一些測(cè)試數(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命令详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: springboot @RequestB
- 下一篇: 怎么把彩色的照片变黑白色怎么把彩色的照片