MongoDB 匯入匯出以及Database Backup

來源:互聯網
上載者:User

標籤:ons   ted   cto   div   恢複   system.in   www   directory   建立   

-------------------MongoDB資料匯入與匯出-------------------1、匯出工具:mongoexport   1.1、概念:        mongoDB中的mongoexport工具可以把一個collection匯出成JSON格式或CSV格式的檔案。可以通過參數指定匯出的資料項目,也可以根據指定的條件匯出資料。    1.2、文法:        mongoexport -d dbname -c collectionname -o file --type json/csv -f field        參數說明:            -d :資料庫名            -c :collection名            -o :輸出的檔案名稱            --type : 輸出的格式,預設為json            -f :輸出的欄位,如果-type為csv,則需要加上-f "欄位名"   1. 3、樣本:  [[email protected] bin]# ./mongoexport -d my_mongodb -c user -o user.dat
  connected to: 127.0.0.1
  exported 2 records
  [[email protected] bin]# cat user.dat
  { "_id" : { "$oid" : "4f81a4a1779282ca68fd8a5a" }, "uid" : 2, "username" : "Jerry", "age" : 100 }
  { "_id" : { "$oid" : "4f844d1847d25a9ce5f120c4" }, "uid" : 1, "username" : "Tom", "age" : 25 }
  [[email protected] bin]#     ....cvs  [[email protected] bin]# ./mongoexport -d my_mongodb -c user --csv -f uid,username,age -o
  user_csv.dat
  connected to: 127.0.0.1
  exported 2 records
  [[email protected] bin]# cat user_csv.dat
  uid,username,age
  2,"Jerry",100
  1,"Tom",25
  [[email protected] bin]# 2、資料匯入:mongoimport    2.1、文法:        mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field        參數說明:            -d :資料庫名            -c :collection名            --type :匯入的格式預設json            -f :匯入的欄位名            --headerline :如果匯入的格式是csv,則可以使用第一行的標題作為匯入的欄位            --file :要匯入的檔案     2.2、樣本:  2.2.1 匯入JSON 資料
  我們先將表user 刪除掉,以便示範效果
  > db.user.drop();
  true
  > show collections;
  system.indexes
  >
  然後匯入資料
  [[email protected] bin]# ./mongoimport -d my_mongodb -c user user.dat
  connected to: 127.0.0.1
  imported 2 objects
  [[email protected] bin]#
  可以看到匯入資料的時候會隱式建立表結構

  2.2.2 匯入CSV資料
  我們先將表user 刪除掉,以便示範效果
  > db.user.drop();
  true
  > show collections;
  system.indexes
  >
  然後匯入資料
  [[email protected] bin]# ./mongoimport -d my_mongodb -c user --type csv --headerline --file
  user_csv.dat
  connected to: 127.0.0.1
  imported 3 objects
  [[email protected] bin]#
  參數說明:
   -type 指明要匯入的檔案格式
   -headerline 批明不匯入第一行,因為第一行是列名
   -file 指明要匯入的檔案路徑
  注意:
  CSV 格式良好,主流資料庫都支援匯出為CSV 的格式,所以這種格式非常利於異構資料移轉        - -------------------MongoDB備份與恢複-------------------1、MongoDBDatabase Backup    1.1、文法:        mongodump -h dbhost -d dbname -o dbdirectory        參數說明:            -h: MongDB所在伺服器位址,例如:127.0.0.1,當然也可以指定連接埠號碼:127.0.0.1:27017            -d: 需要備份的資料庫執行個體,例如:test            -o: 備份的資料存放位置,例如:/home/mongodump/,當然該目錄需要提前建立,這個目錄裡面存放該資料庫執行個體的備份資料。    1.2、執行個體:      [[email protected] bin]# ./mongodump -d my_mongodb -o my_mongodb_dump 2、MongoDB資料庫恢複    2.1、文法:        mongorestore -h dbhost -d dbname --dir dbdirectory         參數或名:            -h: MongoDB所在伺服器位址            -d: 需要恢複的資料庫執行個體,例如:test,當然這個名稱也可以和備份時候的不一樣,比如test2            --dir: 備份資料所在位置,例如:/home/mongodump/itcast/            --drop: 恢複的時候,先刪除當前資料,然後恢複備份的資料。就是說,恢複後,備份後添加修改的資料都會被刪除,慎用!    2.2、執行個體:   [[email protected] bin]# ./mongorestore -d my_mongodb my_mongodb_dump/* 參見:http://www.cnblogs.com/qingtianyu2015/p/5968400.html   http://blog.csdn.net/sxb0841901116/article/details/39894041

MongoDB 匯入匯出以及Database Backup

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.