MongoDB的匯入匯出(7)

來源:互聯網
上載者:User

標籤:

匯入/匯出可以操作的是本地的mongodb伺服器,也可以是遠端.

所以,都有如下通用選項:

-h host   主機

--port port    連接埠

-u username 使用者名稱

-p passwd   密碼

 

mongoexport 匯出json格式的檔案

問: 匯出哪個庫,哪張表,哪幾列,哪幾行?

 -d  庫名

-c  表名

-f  field1,field2...列名

-q  查詢條件

-o  匯出的檔案名稱

-- csv  匯出csv格式(便於和傳統資料庫交換資料)

 

例:

[[email protected] mongodb]# ./bin/mongoexport -d test -c news -o test.json

connected to: 127.0.0.1

exported 3 records

[[email protected] mongodb]# ls

bin  dump  GNU-AGPL-3.0  README  test.json  THIRD-PARTY-NOTICES

[[email protected] mongodb]# more test.json

{ "_id" : { "$oid" : "51fc59c9fecc28d8316cfc03" }, "title" : "aaaa" }

{ "_id" : { "$oid" : "51fcaa3c5eed52c903a91837" }, "title" : "today is sataday" }

{ "_id" : { "$oid" : "51fcaa445eed52c903a91838" }, "title" : "ok now" }

 

 

例2: 只匯出goods_id,goods_name列

./bin/mongoexport -d test -c goods -f goods_id,goods_name -o goods.json

 

例3: 只匯出價格低於1000元的行

./bin/mongoexport -d test -c goods -f goods_id,goods_name,shop_price -q ‘{shop_price:{$lt:200}}’ -o goods.json

 

注: _id列總是匯出

 

mongoimport 匯入

-d 待匯入的資料庫

-c 待匯入的表(不存在會自己建立)

--type csv/json(預設)

--file 備份檔案路徑

例1: 匯入json

./bin/mongoimport -d test -c goods --file./goodsall.json

例2: 匯入csv

./bin/mongoimport -d test -c goods --typecsv -f goods_id,goods_name --file ./goodsall.csv

 

./bin/mongoimport -d test -c goods --typecsv --headline -f goods_id,goods_name --file ./goodsall.csv

 

 

mongodump 匯出二進位bson結構的資料及其索引資訊

-d  庫名

-c  表名

-f field1,field2...列名

 

例:

mongodump -d test  [-c 表名]  --out 匯出路徑

 

規律:

1:匯出的檔案放在以database命名的目錄下

2: 每個表匯出2個檔案,分別是bson結構的資料檔案(比如:goods.bson), json的索引資訊(比如:goods.metadata.json)

3: 如果不聲明表名, 匯出所有的表

例如:mongodump-d test --out C:\Users\yuxl\Desktop\mongodb\mongod\mongodumptest

 

 

mongorestore 匯入二進位檔案

例:

 ./bin/mongorestore -d test --dir 路徑

例如:mongorestore-d test --dir C:\Users\yuxl\Desktop\mongodb\mongodumptest\test

二進位備份,不僅可以備份資料,還可以備份索引,

備份資料比較小.

MongoDB的匯入匯出(7)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.