MongoDB Gridfs operation

Source: Internet
Author: User
Tags findone mongo binary mongoclient mongodb collection

Gridfs Introduction

Gridfs is a MONGODB specification for storing and retrieving large files, slices, audio files, video files, etc. This is a file system used to store files, but the data is stored in a MongoDB collection. Gridfs storage file is larger than its document size 16MB limit.

Gridfs divides a file into blocks to store data for each block in a single file, each with a maximum size of 255K.

Gridfs default uses two collections Fs.files and fs.chunks to store metadata and blocks for the file. Each set of blocks identifies its unique _id objectid field. Fs.files cut off as a parent file. Fs.chunks document files_id A field link block to its parent file.

The following is a sample file for the Fs.files collection:

{   "FileName": "Test.txt", : numberint (261120  "uploaddate" : isodate ( "2014-04-13t11:32:33.557z"   "MD5" Span class= "pun" >:  "7b762939321e146569b07f72c62cca4f" ,< Span class= "PLN" >  "Length" : numberint< Span class= "pun" > (646) }    

Files specify the file name, block size, upload date, and length.

The following is a sample file for the Fs.chunks file:

{   "files_id":ObjectId("534a75d19f54bfec8a2fe44b"),"n":numberint (0),"Data":"Mongo Binary data"}         
To add a file to the Gridfs:

Now we'll use Gridfs's put command to store the MP3 file. To do this, we will use the Mongofiles.exe tool that exists in the Bin folder under the installation folder of MongoDB.

Open a command prompt, navigate to Mongofiles.exe in the Bin folder under the MongoDB installation folder, and type the following code:

>mongofiles.  -D Gridfs put song.  MP3     

Here, Gridfs the database name on the database that is stored in the file. If the database does not exist, MongoDB automatically creates a new document dynamically. Song.mp3 is the name of the uploaded file. To view the files for a database file, you can use the query to find:

>db.  FS.  Files.  Find()       

The above command returns the following document:

{_id: ObjectId ' 534a811bf8b4aa4d33fdf94d '  filename< Span class= "pun" >:  "Song.mp3" , chunksize< Span class= "pun" >: 261120, uploaddate< Span class= "pun" >: new date 1397391643474 Md5:   "e4f53379c909f7bed2e9d631e15c1c41" ,: 10401959 }   

We can also see that the Fs.chunks collection involves using the following code to save the file that exists in the block, using the previous query to return the document ID:

>db.  FS.  Chunks.  Find({files_id:ObjectId(' 534a811bf8b4aa4d33fdf94d ')})    

In my example, the query returns 40 documents, which are 40 pieces of data divided into the entire MP3 file.

Objective

Gridfs is a file specification for storing large files in MongoDB:

The database supports saving binary objects in Bson format. However, the maximum number of Bson objects in MongoDB cannot exceed 4MB.

The GRIDFS specification provides a transparent mechanism for splitting a large file into multiple smaller documents.

To achieve this, the specification specifies a standard for chunking files. Each file holds a metadata object in the file collection object, and one or more block objects can be grouped together in a block collection.

Uploading files

    • Shell

Mongofiles put Xxx.txt

Common parameters are as follows:

-D Specify Database

-u–p Specify user name, password

-h Specifies the host

-port Specifying host Ports

-R If there is a file with the same name, delete other files with the same name

With this command, you can upload files to the FS collection.

    • Java

Get DB

Mongoclient mclient = new Mongoclient ("10.211.55.8");

db = Mclient.getdb ("test");

Get Gridfs Object

Gridfs fs = new Gridfs (db);

Access the files you want to upload

File File = new file ("/users/luoaz/05testdir/files/test1.txt");

Execution hold

Gridfsinputfile gffile = fs.createfile (file);

Gffile.save ();

Download file

    • Shell

Mongofiles Get Xxx.txt

Common parameters are as follows:

-D Specify Database

-u–p Specify user name, password

-h Specifies the host

-port Specifying host Ports

-L download to local file name, default and database name consistent

With this command, you can download files from the FS collection to local.

    • Java

The focus of Java downloads is how to get to the file stream, and then get to the file stream just as normal to keep the file locally.

Get DB

Mongoclient mclient = new Mongoclient ("10.211.55.8");

db = Mclient.getdb ("test");

Get Gridfs Object

Gridfs fs = new Gridfs (db);

The path to the file to download to

File File = new file ("/users/files/down_test1.txt");

FileOutputStream OS = new FileOutputStream (file);

Get file stream

InputStream is = Fs.findone (new Basicdbobject ("filename", "Test1.txt")). getInputStream ();

Download

byte[] bytes = new byte[1024];

while (Is.read (bytes) >0) {

Os.write (bytes);

}

Os.flush ();

Os.close ();

deleting files

    • Shell

Mongofiles Delete Xxx.txt

Deletes a file of the specified name, if there is a file with the same names

Common parameters are as follows:

-D Specify Database

-u–p Specify user name, password

-h Specifies the host

-port Specifying host Ports

    • Java

Get DB

Mongoclient mclient = new Mongoclient ("10.211.55.8");

db = Mclient.getdb ("test");

Get Gridfs Object

Gridfs fs = new Gridfs (db);

Delete file with specified file name

Fs.remove ("Test1.txt");

View Files

    • Shell

1. List

Show All Files

Mongofiles List Xx.txt

Displays all files of the specified file name

2. Search

Searches for files of the specified name, which can be blurred

Mongofiles List Xx.txt

Mongofiles list. txt

    • Java

This is just like MongoDB's find usage.

/*//Saving files
Gridfsfile file = myfs.createfile (new file ("D:/aa.png"));
File.save (); */

Output file
Gridfsdbfile file1 =myfs.findone ("Aa.png");
File1.writeto (New File ("D:/image1.png"));

deleting files
/*gridfsdbfile file =myfs.findone ("image1.jpg");
Myfs.remove ((ObjectId) File2.getid ()); * *

MongoDB Gridfs operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.