MongoDB導入導出以及數(shù)據(jù)庫備份

-------------------MongoDB數(shù)據(jù)導入與導出-------------------

1、導出工具:mongoexport

1、概念:

    mongoDB中的mongoexport工具可以把一個collection導出成JSON格式或CSV格式的文件。可以通過參數(shù)指定導出的數(shù)據(jù)項,也可以根據(jù)指定的條件導出數(shù)據(jù)。

2、語法:

    mongoexport -d dbname -c collectionname -o file --type json/csv -f field

    參數(shù)說明:

        -d :數(shù)據(jù)庫名

        -c :collection名

        -o :輸出的文件名

        --type : 輸出的格式,默認為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ù)導入:mongoimport

1、語法:

    mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field

    參數(shù)說明:

        -d :數(shù)據(jù)庫名

        -c :collection名

        --type :導入的格式默認json

        -f :導入的字段名

        --headerline :如果導入的格式是csv,則可以使用第一行的標題作為導入的字段

        --file :要導入的文件

2、示例:

    sudo mongoimport -d mongotest -c users --file /home/mongodump/articles.json --type json

-------------------MongoDB備份與恢復-------------------

1、MongoDB數(shù)據(jù)庫備份

1、語法:

    mongodump -h dbhost -d dbname -o dbdirectory

    參數(shù)說明:

        -h: MongDB所在服務器地址,例如:127.0.0.1,當然也可以指定端口號:127.0.0.1:27017

        -d: 需要備份的數(shù)據(jù)庫實例,例如:test

        -o: 備份的數(shù)據(jù)存放位置,例如:/home/mongodump/,當然該目錄需要提前建立,這個目錄里面存放該數(shù)據(jù)庫實例的備份數(shù)據(jù)。

2、實例:

    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ù)庫恢復

1、語法:

    mongorestore -h dbhost -d dbname --dir dbdirectory

    參數(shù)或名:

        -h: MongoDB所在服務器地址

        -d: 需要恢復的數(shù)據(jù)庫實例,例如:test,當然這個名稱也可以和備份時候的不一樣,比如test2

        --dir: 備份數(shù)據(jù)所在位置,例如:/home/mongodump/itcast/

        --drop: 恢復的時候,先刪除當前數(shù)據(jù),然后恢復備份的數(shù)據(jù)。就是說,恢復后,備份后添加修改的數(shù)據(jù)都會被刪除,慎用!

2、實例:

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輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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