Click here to Skip to main content
12,759,971 members (32,541 online)
Click here to Skip to main content
Articles » Database » NoSQL » General » Downloads

Tagged as

Stats

105.1K views
2.8K downloads
68 bookmarked
Posted 8 Jan 2013

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

, 9 Jan 2013 CPOL
Highlights the latest developments in both the Mongo open-source document database and the open-source official C# driver.
article
Car.png
Demo.zip
Hadron.png
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
AspFileDataCache.dat
RecentFiles.dat
repSetDb0.cfg
repSetDb1.cfg
repSetDb2.cfg
TestMongo.suo
bin
Debug
mars.png
Thumbs.db
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
TestMongo.csprojResolveAssemblyReference.cache
TestMongo.exe
TestMongo.pdb
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

You may also be interested in...

Pro
Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 9 Jan 2013
Article Copyright 2013 by George Swan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid