MongoDB Study Notes (5) MongoDB File Access Operations

Source: Internet
Author: User
Tags findone
Because MongoDB's document structure is in bjson format (bjson Full name: Binary JSON), bjson itself supports storing binary data, therefore, the binary data of the file can be directly stored in the document structure of MongoDB. However, because the maximum length of a bjson file cannot exceed 4 MB, the maximum size of files that can be stored in a single document cannot exceed 4 MB. To provide support for large-capacity file access, the samus Driver provides the "gridfs" method. The "gridfs" method requires the introduction of new Program Set "MongoDB. gridfs. dll ". The following two methods are used for implementation.
1. access a file in a Document Object

When the file size is small, it is simpler to directly store the file object. For example, the access to a large number of image files is generally not more than 4 MB. The following example shows how to store an uploaded image to a database and write it back to the page:

1. Save the image to bjson

01 /// <Summary>
02 /// Save the image to bjson
03 /// </Summary>
04 Public Void Saveimgbjson (Byte[] Byteimg)
05 {
06 Document Doc =New Document ();
07 Doc ["ID"] = 1;
08 Doc ["IMG"] = Byteimg;
09 Collections collection. Save (DOC );
10 }

2. Obtain the byte data of images stored in bjson format

1 /// <Summary>
2 /// Obtain the byte data of the image stored in bjson format
3 /// </Summary>
4 Public Byte[] Getimgbjson ()
5 {
6 Document Doc = collections collection. findone (New Document {{"ID", 1 }});
7 Return Doc ["IMG"]As Binary;
8 }

The above two paragraphsCodeThis is the two methods added to the BLL encapsulation class for MongoDB-related operations. You can view the content of the previous section in the encapsulation mode. The following describes how to call webform:

Drag a fileupload control and a button control on the interface. Add the following method to the CS class on the page:

1 Protected Void Button#click (Object Sender, eventargs E)
2 {
3 Imgbll =New Imgbll ();
4 Imgbll. deleteall ();
5 Imgbll. saveimgbjson (fileupload1.filebytes );
6 Response. binarywrite (imgbll. getimgbjson ());
7 }
2. Access files through gridfs

Before implementing the gridfs method, let's talk about its principle and why it can store large files. The driver first creates two sets in the current database: "FS. files "and" FS. chunks "set. The former records basic information such as the file name, File Creation Time, and file type. The latter stores binary data of the file in blocks (and supports encryption of these binary data ). Block splitting means to split the file according to the specified size and store it in multiple documents. "Fs. Files": How do you know the binary data of the corresponding file? That's because there is a "files_id" key in "fs. chunks", which corresponds to "_ id" of "fs. Files ". "Fs. chunks" also has a key (INT type) "N", which indicates the order of these blocks. The "FS" in the two collection names can also be customized by parameters.

If you just want to know how to use it, you can ignore the above section and use it as follows:

1. Create, read, and delete gridfs files

01 Private String Gridfssave (Byte[] Bytefile)
02 {
03 String Filename = guid. newguid (). tostring ();
04  
05 // Here, the gridfile constructor has an overload. The bucket parameter is used to replace the default "FS" in the created set name.
06 Gridfile =New Gridfile (relational database );
07 Using (Gridfilestream = gridfile. Create (filename ))
08 {
09 Gridfilestream. Write (bytefile, 0, bytefile. Length );
10 }
11 Return Filename;
12 }
13  
14 Private Byte[] Gridfsread (String Filename)
15 {
16 Gridfile =New Gridfile (relational database );
17 Gridfilestream = gridfile. openread (filename );
18 Byte[] Bytes =New Byte[Gridfilestream. Length];
19 Gridfilestream. Read (bytes, 0, bytes. Length );
20 Return Bytes;
21 }
22  
23 Private Void Gridfsdelete (String Filename)
24 {
25 Gridfile =New Gridfile (relational database );
26 Gridfile. Delete (New Document ("FILENAME", Filename ));
27 }

2. Re-encapsulate the gridfs operation. The new file only stores the file name, which is equivalent to a key. The new file can also have a key other than the "file name.

01 /// <Summary>
02 /// Save the image to gridfs
03 /// </Summary>
04 Public Void Saveimggridfs (Byte[] Byteimg)
05 {
06 String Filename = gridfssave (byteimg );
07  
08 Document Doc =New Document ();
09 Doc ["ID"] = 1;
10 Doc ["FILENAME"] = Filename;
11 Collections collection. Save (DOC );
12 }
13  
14 /// <Summary>
15 /// Obtain images stored in gridfs Mode
16 /// </Summary>
17 Public Byte[] Getimggridfs ()
18 {
19 Document Doc = collections collection. findone (New Document {{"ID", 1 }});
20 String Filename = Doc ["FILENAME"]. Tostring ();
21 Return Gridfsread (filename );
22 }
Iii. Summary

File access should not be difficult. It is worth noting that when reading binary data from the document in the first way, the type must be converted to the "binary" type; there is also the built-in key "_ id", which is not of the string type and is of the "oid" type.

Source: http://www.cnblogs.com/lipan/archive/2011/03/21/1989409.html

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.