290112011 发表于 2018-10-26 08:44:38

MongoDB GridFS 初探

  使用GridFS简单介绍:
  列出文件:
  # mongofiles list
  connected to: 127.0.0.1
  
  
  此时并没有任何文件存在。
  上传文件:
  # 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!
  
  再次列出上传的文件:
  # mongofiles list -d libraray
  connected to: 127.0.0.1
  install.log    9458
  查看MongoDB中的数据:
  # 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 }
  搜索文件:
  # cat testfile
  hello abc
  # 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!
  # mongofiles search test -d libraray
  connected to: 127.0.0.1
  testfile    10
  (貌似无法搜索文件内容!!!)
  删除文件:
  # mongofiles delete testfile -d libraray
  connected to: 127.0.0.1
  done!
  # mongofiles search test -d libraray
  connected to: 127.0.0.1
  获取文件:
  # ls
  anaconda-ks.cfginstall.loginstall.log.syslogmodbtestfile
  # rm -rf install.log
  # mongofiles get install.log -d libraray
  connected to: 127.0.0.1
  done write to: install.log
  # ls
  anaconda-ks.cfginstall.loginstall.log.syslogmodbtestfile
  
  测试上传大文件:
  # du -h 2290944.zip
  3.2G    2290944.zip
  # mongofiles put2290944.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>
页: [1]
查看完整版本: MongoDB GridFS 初探