MongoDB Learning Notes (v) MongoDB file access Operations

Source: Internet
Author: User
Tags findone mongodb
Because the document structure of MongoDB is in the Bjson format (bjson full name: Binary JSON), and the Bjson format itself supports data in binary format, you can save the data in binary format of the file directly into the MongoDB document structure. However, because the maximum length of a bjson cannot exceed 4M, it limits the maximum number of files that can be deposited in a single document beyond 4M. To provide support for bulk file access, the Samus driver provides a "GRIDFS" approach to support, and "Gridfs" mode file operations require the introduction of a new assembly "MongoDB.GridFS.dll". Here are two ways to implement each. first, Access files in the Document object

When the file size is small, directly into the document object to achieve more concise. For example, a large number of image file access, general picture files will not exceed 4M. We first implement an upload image into the database, and then take out to write back to the page example:

1. Save the picture to Bjson view source print?

01 <summary>
02 Save the picture in the Bjson
03 </summary>
04 public void Saveimgbjson (byte[] byteimg)
05 {
06 Document doc = new document ();
07 doc["ID"] = 1;
08 doc["Img"] = byteimg;
09 Mongocollection.save (DOC);
10 }

2. Get the picture byte data stored bjson mode view source print?

1 <summary>
2 Get picture byte data stored in Bjson mode
3 </summary>
4 Public byte[] Getimgbjson ()
5 {
6 Document Doc= Mongocollection.findone (new document {{"ID", 1}});
7 return doc["IMG"] as Binary;
8 }

The above two pieces of code are two methods added to the BLL encapsulation class for MONGODB related operations to encapsulate the contents of the previous section. Here's a look at how to call in WebForm:

In the interface, drag out a FileUpload control and a button control, Page CS class Add the following method: View source print?

1 protected void Button1_Click (object sender, EventArgs e)
2 {
3 IMGBLL IMGBLL = new IMGBLL ();
4 Imgbll.deleteall ();
5 Imgbll.saveimgbjson (fileupload1.filebytes);
6 Response.BinaryWrite (Imgbll.getimgbjson ());
7 }
second, the use of Gridfs way to access files

Before realizing the Gridfs way, I'll talk about its principle and why I can save large files. The driver first creates two collections in the current database: the "Fs.files" and "Fs.chunks" collections, which record file names, file creation times, file types, and other basic information, which blocks the binary data of the file (and supports encrypting the binary data). Chunking means dividing a file by a specified size and then depositing it into multiple documents. "Fs.files" How to know it corresponds to the file binary data in which blocks. That is because there is a "files_id" key in "Fs.chunks", which corresponds to the "_id" of "fs.files". "Fs.chunks" also has a key (int type) "n", which indicates the order of the blocks. The "FS" in the two collection names can also be customized by parameters.

If you just want to know how to use, you can ignore the above paragraph, the following will use:

1. Gridfs Way of File New, read, delete view source print?

01 private string Gridfssave (byte[] bytefile)
02 {
03 string filename = Guid.NewGuid (). ToString ();
04
05 Here the Gridfile constructor has an overload, and the bucket parameter is used to replace the default "FS" in the Create collection name.
06 Gridfile gridfile = new Gridfile (mongodatabase);
07 using (Gridfilestream 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 gridfile = new Gridfile (mongodatabase);
17 Gridfilestream gridfilestream = gridfile.openread (filename);
18 byte[] bytes = new Byte[gridfilestream.length];
19 Gridfilestream.read (bytes, 0, bytes. Length);
20 return bytes;
21st }
22
23 private void Gridfsdelete (string filename)
24 {
25 Gridfile gridfile = new Gridfile (mongodatabase);
26 Gridfile.delete (New Document ("filename", filename));
27 }

2. Encapsulates the Gridfs operation again, the new document only stores the file name, equivalent to just a key, the new document can also have other than "filename" other keys. View Source print?

01 <summary>
02 Save the picture in the 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 Mongocollection.save (DOC);
12 }
13
14 <summary>
15 Get pictures stored in Gridfs mode
16 </summary>
17 Public byte[] Getimggridfs ()
18 {
19 Document doc = Mongocollection.findone (new Document {{"ID", 1}});
20 string filename = doc["filename"]. ToString ();
21st return gridfsread (filename);
22 }
Third, summary

File access should not be difficult, it is worth noting that when you read binary data from a document in the first way, be sure to convert the type to the "Binary" type, and the system's own key "_id", which is not a string type, is "Oid" type.

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.