Use of MongoDb Gridfs

Source: Internet
Author: User

MongoDB Gridfs is a MongoDB file storage scheme that is primarily used to store and restore files that exceed 16M (Bson file limits), such as pictures, audio, etc., and have better performance for large files.

To use Gridfs in C #, first install the NuGet package: MongoDB.Driver.GridFS

Buckets

The data in Gridfs is also stored in sub-collections, each set is called a bucket, each bucket can store multiple files:

Using buckets in C # is similar to using a collection, creating a Gridfsbucket object.

var  Bucket = new  gridfsbucketnew  gridfsbucketoptions ()
{
    bucketname     =  "My_bucket"     chunksizebytes =  256 * 1024, // block size
});

For buckets, the main parameter is the bucket name and block size, which is not created manually and is created automatically when used.

Upload Data :

For byte[] arrays, you can upload directly using uploadfrombytes :

var data = Enumerable.Range(0, 256).Select(i => (byte)i).ToArray();
var id = bucket.UploadFromBytes("filename", data);

The primary parameter is to pass in a file name (the file name does not require a unique, the same bucket class can be duplicated) and returns the data ID.

You can also bring in metadata information.

VarOptions =NewGridfsuploadoptions
{
Metadata=NewBsondocument
{
{"Resolution", "1080P"},
{"copyrighted", True}
},
DisableMD5 = true,
};
var id = bucket. Uploadfrombytes("filename", data, options);

Upload Stream:

When using uploadfrombytes upload, you need to read all the uploaded data to memory, if you need to upload a larger file, this method is not appropriate. This can be uploaded using the uploadfromstream function.

using (var fs = File.OpenRead(@"r:\123.jpg"))
{
    bucket.UploadFromStream("123.jpg", fs);
}

You can also use Openuploadstream to open as a stream and then push to complete the upload.

Using(VarFS =File.OpenRead(@ "R:\123.jpg"   (var upload = bucket. Openuploadstream ( {
    fs. Copy     upload. Close     console. Writeline} /span>

Note: This stream must be manually close, but the call to Dispose will not be written to the bucket . I don't know if it counts as a bug in the MongoDB API.

Download:

The downloaded API is similar to the upload, and here are a few basic examples:

VarID =NewObjectId("5b6ba04c77850928a438b1b2");
Varbytes = Bucket.Downloadasbytes(ID);

Using(Vartarget =File.Create(@ "R:\target.jpg"))
{
Bucket.Downloadtostream(ID, target);
}

Using (var download = bucket. Opendownloadstream(ID))
Using (var target = File. Create(@ "r:\target.jpg"))
{
Download. CopyTo(target);
}

Inquire:

Gridfs uses two sets to store a file: Fs.files and Fs.chunks. The records in buckets are actually stored in two parts.

    • The actual contents of the file are present in chunks (binary data) and split into a bunch of chunk storage
    • Related description information in the Files collection, it is a standard MongoDB document model with the structure of:
{  "_id": <ObjectId>,  "length": <num>,  "chunkSize": <num>,  "uploaddate": <timestamp>,  "MD5": ,  "filename": <string>,  "ContentType": <string>,  "aliases": <stringArray>,  "metadata": <any>,}
< Span style= "color:black;" > < Span style= "color: #023264;" > < Span style= "color: #019000;" >gridfs to filename And the Uploaddate fields are indexed, and they can be queried for better performance.  

Reference Document: https://docs.mongodb.com/manual/core/gridfs/

The Gridfs API encapsulates the query for it, and also follows the standard query API.

var filter = new {filename = "123.jpg"};
var result = bucket.Find(filter.ToBsonDocument()).ToList();

Alternatively, you can manipulate the Fs.files collection directly.

var files = db. GetCollection<bsondocument> ("My_bucket.files");

Update:

Gridfs's API supports renaming documents:

bucket.Rename(id, "NewName");

But did not find a hit update metadata API, on the Internet to find a bit, someone has already mentioned this problem. The conclusion is that there is no official encapsulation at the moment, but you can update metadata directly from the Fs.files collection. Examples are shown below.

VarFiles = db.GetCollection<Bsondocument> ("My_bucket.files");

VarFilter =NewBsondocument()
{
[] = id
};
var update =  new bsondocument< Span style= "color:black;" > ()
{
    [] =  new  Bsondocument ()
    {
         [ "metadata" ] = metadata
    }
};

Files. Updateone

Summary

Gridfs API encapsulation is also useful, but do not know why not encapsulate the description of the information update operation. Official documentation links are as follows, to learn more can be viewed.

    • Getting Started
    • Uploading files
    • Downloading files
    • Finding files
    • Deleting and renaming files

This article mainly describes the use of Gridfs, but it is important to note that the GRIDFS itself is not a distributed storage service, it still relies on mongodb, not to solve the problem of large-scale distributed storage, Scenarios such as mass storage and load balancing are required or given to professional services such as Fastdfs.

However, for some enterprise applications where performance and capacity requirements are low, it is appropriate to store small-scale storage scenarios such as images and attachments. Deployment and use are very quick and easy.

In addition, there are some in-depth on the internet to introduce GRIDFS documents, you can also see.

    • http://shift-alt-ctrl.iteye.com/blog/2195646
    • Https://juejin.im/entry/57fc27492e958a00559926d5
    • https://www.imooc.com/article/43502

Use of MongoDb Gridfs

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.