Click here to Skip to main content
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
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
bin
Debug
mars.png
Thumbs.db
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
TestMongo.csprojResolveAssemblyReference.cache
TestMongo.exe
TestMongo.pdb
namespace TestMongo
{
    using System;

    using MongoDB.Driver;
    using MongoDB.Driver.Builders;

    public partial class Program
    {
        #region Methods

        private static void Main()
        {
          
            // const string connectionString = "mongodb://myUserName:MyPassword@linus.mongohq.com:myPortNumber/";
            const string connectionString = "mongodb://localhost/?replicaSet=myReplSet&readPreference=primary";
           // const string connectionString = "mongodb://localhost";
          
            // Create a MongoClient object by using the connection string
            var client = new MongoClient(connectionString);

            //Use the MongoClient to access the server
            MongoServer server = client.GetServer();

            // Use the server to access the 'test' database
            MongoDatabase database = server.GetDatabase("test");
     
            //Builds new Collection if 'entities' is not found
            MongoCollection<ClubMember> collection = database.GetCollection<ClubMember>("entities");

            long count = collection.Count();

            var random = new Random();
            if (count == 0)
            {
                Console.WriteLine("Collection is empty.");
                Console.WriteLine("Enter the number of people to add");

                string ans = Console.ReadLine();
                int counter;
                int.TryParse(ans, out counter);
                Console.WriteLine("Adding {0} new ClubMembers", counter);

                BuildClubMembers(collection, random, counter);
            }
            else
            {
                Console.WriteLine("Number of ClubMembers in Collection {0}", count);
            }

            //Build an index if it is not already built
            IndexKeysBuilder keys = IndexKeys.Ascending("Lastname", "Forename").Descending("Age");
           
            //Add an optional name- useful for admin
            IndexOptionsBuilder options = IndexOptions.SetName("myIndex");
          
            //This locks the database while the index is being built
            collection.EnsureIndex(keys, options);

            Console.WriteLine("List of ClubMembers in collection ...");
            MongoCursor<ClubMember> members = collection.FindAll();
            foreach (ClubMember clubMember in members)
            {
                clubMember.PrintDetailsToScreen();
            }

            DemoLinq(collection);
            DemoQueryBuilder(server,database,collection);
            DemoAggregation(collection);
            DemoMapReduce(collection);
            DemoGridFS(database);


            Console.WriteLine("Press return key to exit or");
            Console.WriteLine("enter 'yes' to empty the Database and exit");
            string emptyDb = Console.ReadLine();
            if (emptyDb == "yes")
            {
                collection.RemoveAll();
            }
        }

        #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

You may also be interested in...

About the Author

George Swan
Student
Wales Wales
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 9 Jan 2013
Article Copyright 2013 by George Swan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid