GridFS and the 1.6 C# driver, examples lacking.

428 views
Skip to first unread message

John Cole

unread,
Oct 7, 2012, 6:06:45 PM10/7/12
to mongodb...@googlegroups.com
I'm trying to use the 1.6 C# driver to store images into a GridFS (MongoDB 2.2), but nearly all of the examples reference the same Stackoverflow post, and it's really simplistic.

I have a Bitmap, and can save it using the GridFS.Upload function but thats simply using mongo as a file system.  I'd like to take advantage of being able to query the images for other properties, such as a version number or something.

Here is the code that saves an image so far:

MemoryStream memory = new MemoryStream();

bitmap.Save(memory, ImageFormat.Png);

var d = db.GridFS.Upload(memory, p.name);


What I'd like to be able to do is Create a GridFS file stream, and have the bitmap save directly into it, and tag the image with a version number and other information.  Then I'd like to be able to query the file by name and version number and turn it back into a bitmap or stream.

I haven't found an example on how one would do this?  Anyone have some advice?

Thanks,

John

Daniele Tassone

unread,
Oct 8, 2012, 12:03:52 PM10/8/12
to mongodb...@googlegroups.com, John Cole
Il 08/10/2012 00:06, John Cole ha scritto:

What I'd like to be able to do is Create a GridFS file stream, and have the bitmap save directly into it, and tag the image with a version number and other information.  Then I'd like to be able to query the file by name and version number and turn it back into a bitmap or stream.

I haven't found an example on how one would do this?  Anyone have some advice?

Thanks,

John

// metadata = your custom object
// myMongoGrid = your GridFS object
// nome_file = your file name
MongoGridFSCreateOptions cOption = new MongoGridFSCreateOptions(); 
cOption.Metadata = (new { type = "gallery", sub_type = "original", w = w, format = format }).ToBsonDocument();

MongoGridFSFileInfo fi = myMongoGrid.Upload(myMemoryStream, nome_file, cOption); 
fi.Id.AsObjectId // your objectIdInserted

// example query by ID and Resize Width and Format (jpeg,PNG)
MongoGridFSFileInfo fi = myMongoGridFs.FindOne(Query.And(Query.EQ("metadata.OriginalId"new ObjectId(Id)), Query.EQ("metadata.w", w), Query.EQ("metadata.format", image_format)));


Daniele

Reply all
Reply to author
Forward
0 new messages