MongoDB數(shù)據(jù)遷移

需求:將某臺(tái)Linux上的MongoDB數(shù)據(jù)庫(kù)遷移到另一臺(tái)Linux中
步驟:導(dǎo)出mongoexport、導(dǎo)入mongoimport

導(dǎo)出集合

MongoDB中的mongoexport可將集合導(dǎo)出為JSON或CSV格式的文件,指的注意的CSV文件對(duì)于大部分的關(guān)系型數(shù)據(jù)庫(kù)而言是支持的。

$ mongoexport -d db -c collection -o outfile --type json/csv -f fields
-h, --host 遠(yuǎn)程連接的數(shù)據(jù)庫(kù)地址,默認(rèn)連接本地Mongo數(shù)據(jù)庫(kù)。
--port 遠(yuǎn)程連接的數(shù)據(jù)庫(kù)的端口,默認(rèn)為27017.
-u, --username 連接遠(yuǎn)程數(shù)據(jù)庫(kù)的賬戶,若數(shù)據(jù)庫(kù)設(shè)置了認(rèn)證,需指定賬戶。
-p, --password 連接遠(yuǎn)程數(shù)據(jù)庫(kù)賬戶對(duì)應(yīng)的密碼
--authenticationDatabase 認(rèn)證的數(shù)據(jù)庫(kù)
-d, --db 數(shù)據(jù)庫(kù)名稱
-c, --collection 集合名稱
-o, --out 導(dǎo)出的文件名
--type 導(dǎo)出的文件格式,默認(rèn)為JSON,可選CSV、JSON。
-f, --fields 導(dǎo)出的字段,多字段以逗號(hào)分隔,當(dāng)輸出格式為CSV是必須指定輸出的字段,CSV大部分關(guān)系型數(shù)據(jù)庫(kù)都支持。
-q, --query 查詢條件
--skip 跳過(guò)指定數(shù)量的數(shù)據(jù)
--limit 讀取指定數(shù)量的數(shù)據(jù)記錄
--sort 對(duì)數(shù)據(jù)進(jìn)行排序,可指定排序的字段,使用1為升序-1為降序,如 sort({key:1})。

例如:指定查詢條件導(dǎo)出bike集合數(shù)據(jù)為CSV

$ mongoexport -p 27030 -u sa -p sa -d map -c bike -f id,lat,lng,created_at,source -o bike.csv --query='{"source":"ofo"}' --limit=1
# 導(dǎo)出集合
$ mongoexport --host 127.0.0.1 --port 27017 --username sa --password sa --authenticationDatabase game --db game --collection ap_user --type json --out ./ap_user.json
# 簡(jiǎn)化方式
$ mongoexport -d game -c tf_game -o ./tf_game.json

導(dǎo)入集合

# 導(dǎo)入集合
$ mongoimport --host 127.0.0.1 --port 27017 --username sa --password sa --authenticationDatabase game --db game --collection ap_user --file ./tf_game.json 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容