Click here to Skip to main content
11,480,148 members (61,945 online)
Click here to Skip to main content
Articles » Database » NoSQL » General » Downloads
Add your own
alternative version

Beginners' guide to using MongoDB 2.2 and the official C# driver

, 9 Jan 2013 CPOL 57.2K 1.6K 62
Highlights the latest developments in both the Mongo open-source document database and the open-source official C# driver.
article.zip
article
Car.png
Demo.zip
Hadron.png
Demo-noexe.zip
TestMongo
_ReSharper.TestMongo
AspFileDataCache.dat
RecentItems
RecentFiles.dat
repSetDb0.cfg
repSetDb1.cfg
repSetDb2.cfg
TestMongo.suo
TestMongo
Classes
Demos
Images
mars.png
Thumbs.db
Methods
obj
x86
Debug
Properties
Demo.zip
AspFileDataCache.dat
RecentFiles.dat
repSetDb0.cfg
repSetDb1.cfg
repSetDb2.cfg
TestMongo.suo
mars.png
Thumbs.db
TestMongo.exe
namespace TestMongo
{
    using System;
    using System.IO;
    using System.Linq;

    using MongoDB.Bson;
    using MongoDB.Driver;
    using MongoDB.Driver.Builders;
    using MongoDB.Driver.GridFS;

    public partial class Program
    {
        #region Public Methods and Operators

        public static void DemoGridFS(MongoDatabase database)
        {
            var assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            var directoryName = Path.GetDirectoryName(assemblyPath);
            string fullyQualifiedUpLoadName = directoryName+@"\Images\mars.png";

            //The uploaded file name equals fullyQualifiedUpLoadName
            MongoGridFSFileInfo gridFsInfo = database.GridFS.Upload(fullyQualifiedUpLoadName);

            //Here the uploaded file is given the name 'mars.png'
            using (var fs = new FileStream(fullyQualifiedUpLoadName, FileMode.Open))
            {
                database.GridFS.Upload(fs, "mars.png");
            }
            // Add metadata to facilitate searching.
            var photoMetadata = new BsonDocument
            { { "Category", "Astronomy" }, { "SubGroup", "Planet" }, { "ImageWidth", 640 }, { "ImageHeight", 480 } };

            database.GridFS.SetMetadata(gridFsInfo, photoMetadata);

            //Build an index to search the metadata
            MongoCollection<BsonDocument> coll = database.GetCollection("fs.files");
            IndexKeysBuilder keys = IndexKeys.Ascending("metadata.Category", "metadata.SubGroup");

            //Add an optional name- useful for admin
            IndexOptionsBuilder options = IndexOptions.SetName("PhotoIndex");

            //This locks the database while the index is being built
            coll.EnsureIndex(keys, options);

            //Search using the PhotoIndex
            MongoCursor<MongoGridFSFileInfo> astronomyPics =
                database.GridFS.Find(Query.EQ("metadata.Category", "Astronomy"));

            //use a filename to download.
             string fullyQualifiedDownLoadName = directoryName + @"\Images\mars2.png"; ;
            database.GridFS.Download(fullyQualifiedDownLoadName, fullyQualifiedUpLoadName);

            MongoCursor<MongoGridFSFileInfo> filesInfo = database.GridFS.FindAll();
            if (filesInfo.Any())
            {
                Console.WriteLine("The following files have been uploaded.");
            }
            foreach (MongoGridFSFileInfo fileInfo in filesInfo)
            {
                Console.WriteLine(
                    "Name {0} size {1} uploaded on {2}",
                    fileInfo.Name,
                    fileInfo.Length,
                    fileInfo.UploadDate.ToShortDateString());
            }

            //  database.GridFS.Delete(fullyQualifiedUpLoadName);
            //  database.GridFS.Delete("mars.png");
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

George Swan
Student
Wales Wales
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 9 Jan 2013
Article Copyright 2013 by George Swan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid