MongoDB GridFS 初探

來源:互聯網
上載者:User

標籤:gridfs 簡單使用

使用GridFS簡單介紹:

列出檔案:

[[email protected] ~]# mongofiles list
connected to: 127.0.0.1



此時並沒有任何檔案存在。


上傳檔案:

[[email protected] ~]# mongofiles put install.log -d libraray
connected to: 127.0.0.1
added file: { _id: ObjectId(‘5616b8c4f1403ca60b0999d0‘), filename: "install.log", chunkSize: 261120, uploadDate: new Date(1444329668441), md5: "c7d8a01a077940859e773b7770d2e07e", length: 9458 }
done!


再次列出上傳的檔案:

[[email protected] ~]# mongofiles list -d libraray
connected to: 127.0.0.1
install.log    9458



查看MongoDB中的資料:

[[email protected] ~]# mongo libraray
MongoDB shell version: 2.6.11
connecting to: libraray
> db.fs.files.find()
{ "_id" : ObjectId("5616b8c4f1403ca60b0999d0"), "filename" : "install.log", "chunkSize" : 261120, "uploadDate" : ISODate("2015-10-08T18:41:08.441Z"), "md5" : "c7d8a01a077940859e773b7770d2e07e", "length" : 9458 }
>
指定id:

> db.fs.files.find({"_id" : ObjectId("5616b8c4f1403ca60b0999d0")})
{ "_id" : ObjectId("5616b8c4f1403ca60b0999d0"), "filename" : "install.log", "chunkSize" : 261120, "uploadDate" : ISODate("2015-10-08T18:41:08.441Z"), "md5" : "c7d8a01a077940859e773b7770d2e07e", "length" : 9458 }


> db.fs.chunks.find({},{"data":0})
{ "_id" : ObjectId("5616b8c4d2b40c73cbf811c8"), "files_id" : ObjectId("5616b8c4f1403ca60b0999d0"), "n" : 0 }


搜尋檔案:

[[email protected] ~]# cat testfile
hello abc

[[email protected] ~]# mongofiles put testfile -d libraray
connected to: 127.0.0.1
added file: { _id: ObjectId(‘5616bd093be512c2e45d0351‘), filename: "testfile", chunkSize: 261120, uploadDate: new Date(1444330761770), md5: "563427968920fcf5177c6753bd6cbae3", length: 10 }
done!

[[email protected] ~]# mongofiles search test -d libraray
connected to: 127.0.0.1
testfile    10

(貌似無法搜尋檔案內容!!!)

刪除檔案:

[[email protected] ~]# mongofiles delete testfile -d libraray
connected to: 127.0.0.1
done!
[[email protected] ~]# mongofiles search test -d libraray
connected to: 127.0.0.1

擷取檔案:

[[email protected] ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  modb  testfile
[[email protected] ~]# rm -rf install.log
[[email protected] ~]# mongofiles get install.log -d libraray
connected to: 127.0.0.1
done write to: install.log
[[email protected] ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  modb  testfile


測試上傳大檔案:

[[email protected] ~]# du -h 2290944.zip
3.2G    2290944.zip

[[email protected] ~]# mongofiles put  2290944.zip -d libraray
connected to: 127.0.0.1
added file: { _id: ObjectId(‘5616c158bfb7f87bb8e48d44‘), filename: "2290944.zip", chunkSize: 261120, uploadDate: new Date(1444331976703), md5: "6fc45dd991d89db358194eba0b565f30", length: 3372909855 }
done!


> db.fs.chunks.find({"files_id":ObjectId("5616c158bfb7f87bb8e48d44")},{"data":0}).sort({n:-1}).limit(1)
{ "_id" : ObjectId("5616c1a2d2b40c73cbf84467"), "files_id" : ObjectId("5616c158bfb7f87bb8e48d44"), "n" : 12917 }


總共有12918個chunk(第一個chunk為0),將12918 × 261120 size是:3373148160可以存這個檔案,在其它機器中把它重新取回成功。


本文出自 “技術部落格” 部落格,請務必保留此出處http://raytech.blog.51cto.com/7602157/1701254

MongoDB GridFS 初探

相關文章

聯繫我們

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