-------------------MongoDB資料匯入與匯出------------------- 一、資料匯出:mongoexport 1、概念: mongoDB中的mongoexport工具可以把一個collection匯出成JSON格式或CSV格式的檔案,可以通過引數指定匯出的資料項,也可以根據指定的條件匯出資料, 2、語法: mongoexport -d dbname -c collectionname -o file --type json/csv -f field 引數說明: -d :資料庫名 -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" 二、資料匯入:mongoimport 1、語法: mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field 引數說明: -d :資料庫名 -c :collection名 --type :匯入的格式默認json -f :匯入的欄位名 --headerline :如果匯入的格式是csv,則可以使用第一行的標題作為匯入的欄位 --file :要匯入的檔案 2、示例: sudo mongoimport -d mongotest -c users --file /home/mongodump/articles.json --type json -------------------MongoDB備份與恢復------------------- 一、MongoDB資料庫備份 1、語法: mongodump -h dbhost -d dbname -o dbdirectory 引數說明: -h: MongDB所在服務器地址,例如:127.0.0.1,當然也可以指定埠號:127.0.0.1:27017 -d: 需要備份的資料庫實體,例如:test -o: 備份的資料存放位置,例如:/home/mongodump/,當然該目錄需要提前建立,這個目錄里面存放該資料庫實體的備份資料, 2、實體: sudo rm -rf /home/momgodump/ sudo mkdir -p /home/momgodump sudo mongodump -h 192.168.17.129:27017 -d itcast -o /home/mongodump/ - 二、MongoDB資料庫恢復 1、語法: mongorestore -h dbhost -d dbname --dir dbdirectory 引數說明: -h: MongoDB所在服務器地址 -d: 需要恢復的資料庫實體,例如:test,當然這個名稱也可以和備份時候的不一樣,比如test2 --dir: 備份資料所在位置,例如:/home/mongodump/itcast/ --drop: 恢復的時候,先洗掉當前資料,然后恢復備份的資料,就是說,恢復后,備份后添加修改的資料都會被洗掉,慎用! 2、實體: mongorestore -h 192.168.17.129:27017 -d itcast_restore --dir /home/mongodump/itcast/ 出處:https://www.cnblogs.com/qingtianyu2015/p/5968400.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/232424.html
標籤:其他
下一篇:SQL語法
