MongoDB導(dǎo)入/導(dǎo)出/備份

-------------------MongoDB數(shù)據(jù)導(dǎo)入與導(dǎo)出-------------------
1、導(dǎo)出工具:mongoexport
1、概念:
mongoDB中的mongoexport工具可以把一個(gè)collection導(dǎo)出成JSON格式或CSV格式的文件??梢酝ㄟ^參數(shù)指定導(dǎo)出的數(shù)據(jù)項(xiàng),也可以根據(jù)指定的條件導(dǎo)出數(shù)據(jù)。
2、語法:
mongoexport -d dbname -c collectionname -o file --type json/csv -f field
參數(shù)說明:
-d :數(shù)據(jù)庫名
-c :collection名
-o :輸出的文件名
--type : 輸出的格式,默認(rèn)為json
-f :輸出的字段,如果-type為csv,則需要加上-f "字段名"
3、示例:
sudo mongoexport -d mongotest -c users -o /home/python/Desktop/mongoDB/users.json --type json -f "_id,user_id,user_name,age,status"

2、數(shù)據(jù)導(dǎo)入:mongoimport
1、語法:
mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field
參數(shù)說明:
-d :數(shù)據(jù)庫名
-c :collection名
--type :導(dǎo)入的格式默認(rèn)json
-f :導(dǎo)入的字段名
--headerline :如果導(dǎo)入的格式是csv,則可以使用第一行的標(biāo)題作為導(dǎo)入的字段
--file :要導(dǎo)入的文件

2、示例:
    sudo mongoimport -d mongotest -c users --file /home/mongodump/articles.json --type json

-------------------MongoDB備份與恢復(fù)-------------------
1、MongoDB數(shù)據(jù)庫備份
1、語法:
mongodump -h dbhost -d dbname -o dbdirectory
參數(shù)說明:
-h: MongDB所在服務(wù)器地址,例如:127.0.0.1,當(dāng)然也可以指定端口號:127.0.0.1:27017
-d: 需要備份的數(shù)據(jù)庫實(shí)例,例如:test
-o: 備份的數(shù)據(jù)存放位置,例如:/home/mongodump/,當(dāng)然該目錄需要提前建立,這個(gè)目錄里面存放該數(shù)據(jù)庫實(shí)例的備份數(shù)據(jù)。
2、實(shí)例:
sudo rm -rf /home/momgodump/
sudo mkdir -p /home/momgodump
sudo mongodump -h 192.168.17.129:27017 -d itcast -o /home/mongodump/
-
2、MongoDB數(shù)據(jù)庫恢復(fù)
1、語法:
mongorestore -h dbhost -d dbname --dir dbdirectory

    參數(shù)或名:
        -h: MongoDB所在服務(wù)器地址
        -d: 需要恢復(fù)的數(shù)據(jù)庫實(shí)例,例如:test,當(dāng)然這個(gè)名稱也可以和備份時(shí)候的不一樣,比如test2
        --dir: 備份數(shù)據(jù)所在位置,例如:/home/mongodump/itcast/
        --drop: 恢復(fù)的時(shí)候,先刪除當(dāng)前數(shù)據(jù),然后恢復(fù)備份的數(shù)據(jù)。就是說,恢復(fù)后,備份后添加修改的數(shù)據(jù)都會(huì)被刪除,慎用!
2、實(shí)例:
mongorestore -h 192.168.17.129:27017 -d itcast_restore --dir /home/mongodump/itcast/
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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