MongoDB GridFS : Database File Storage

7,910 views

Published on

Learn how to store and retrieve files from the database using Mongo’s GridFS and ProdigyView.

Published in: Technology
  • Be the first to comment

MongoDB GridFS : Database File Storage

  1. 1. MongoDB and GridFS
  2. 2. OverviewObjectiveLearn how to store and retrieve files from the database usingMongo’s GridFS and ProdigyView.Requirements Installed PECL Mongo Extension A Mongo Database Understanding of MongoDB Basics with ProdigyView Understanding of connecting to a database in ProdigyViewEstimated Time10 Minutes
  3. 3. Follow Along With Code Example1. Download a copy of the example code at www.prodigyview.com/source.2. Install the system in an environment you feel comfortable testing in.3. Proceed to examples/database/MongoGridFS.php www.prodigyview.com
  4. 4. What Is GridFSThe GridFS is MongoDB’s way of storing files in thedatabase. The files are stored in binary chunks that arearound 256k in size. File GridFS Chunk Chunk Chunk Chunk Chunk Chunk
  5. 5. File InformationBefore the file is entered into Mongo, we need to setup options andvariables. The data inserted with the file will become the meta data.The data is used to describe the file stored. 1. Insert data will act as the file meta data 2.Set the option to GridFS to 3. Set the file to the location of on the true server
  6. 6. Insert File Then pass the insert data(meta data) and the options to PVDatabase::insertStatement() and the process of putting the file in the database will be handled for you. 1. The collection name 2. The meta data about the file4. Returns a Mongo ID 3. The options for uploading
  7. 7. Searching GridFSSearching in GridFS uses the same syntax as a regularsearch in ProdigyView except the gridFS option has to beset to true.1. Search based on these arguments 2. Set the collection name 3. Set gridFS to true
  8. 8. Getting a Single FileAlso similar is retrieving a single file. The only difference again isto the gridFS option and combine it with the findOne option. 1. Set gridFS to true 2. Set findOne to True
  9. 9. Displaying The ImageIn our example, we uploaded images to the database. Nowwe need to retrieve the image and display it. UsinggetBytes() method, we can read in the chunks from thedocument retrieved from gridFS. This will return a string ofbytes.1. Get the image bytes from the result 2. Write the bytes to file 3. Display the image www.prodigyview.com
  10. 10. Updating GridFS When updating a file in gridFS, we can modify the meta data of the file. The update mechanics work the same way when updating a normal document except the gridFS must be set to true. The file itself will be unaltered.1. Collection name 2. The fields to update 3. Where to update 4. Set gridFS to true
  11. 11. Deleting FileThe final action is deleting a file. On delete, the meta data andchunks be removed from the database. The same syntax fromthe select statement is used. www.prodigyview.com
  12. 12. API ReferenceFor a better understanding of the database, visit the apiby clicking on the link below.PVDatabase More TutorialsFor more tutorials, please visit:http://www.prodigyview.com/tutorials www.prodigyview.com

×