Daniel Robinson
Daniel Robinson

Reputation: 14898

c# mongodb large file within a document

I am not entirely sure how GridFS works in MongoDB. All the examples I have seen currently seen just involve grabbing a file and uploading it to a db through the api, but I want to know

a) can you have large files embedded in your typical JSON style documents or do they have to be stored in their own special GridFS collection or db?

b) how can I handle this type of situation where I have an object which has some typical fields in it, strings ints etc but also has a collection of attachment files which could be anything from small txt files to fairly large video files?

for example

class bug
{
    public int Id { get; protected set; }
    public string Name { get; protected set; }
    public string Description { get; protected set; }
    public string StackTrace { get; protected set; }
    public List<File> Attachments { get; protected set; } //pictures/videos of the bug in action or text files with user config data in it etc.
}

Upvotes: 1

Views: 1766

Answers (1)

Andrew Orsich
Andrew Orsich

Reputation: 53695

a) can you have large files embedded in your typical JSON style documents or do they have to be stored in their own special GridFS collection or db?

You can in case if file size don't goes above the mongodb document size limit in 16mb. But you will need serialize/deserialize and do another extra work yourself.

b) how can I handle this type of situation where I have an object which has some typical fields in it, strings ints etc but also has a collection of attachment files which could be anything from small txt files to fairly large video files?

If you finally decided to store your attachments in mongodb, better way to go with gridfs. You can simple store file in gridfs, but in the Attachments collection store id of this file and any metadata (file name, size, etc.). Then you can easy get file content by id from inner Attachments collection.

Mongodb gridf is a simple layer above mongodb, that can split big files into chunks and store them in mongodb and also read files back from chunks. To get started with c# and gridfs read this answer.

Upvotes: 2

Related Questions