Click here to Skip to main content
13,595,473 members
Click here to Skip to main content
Articles » Database » NoSQL » General » Downloads

Tagged as

Stats

129.3K views
3.1K downloads
80 bookmarked
Posted 9 Jan 2013
Licenced CPOL

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

, 19 Nov 2017
Highlights the latest developments in both the Mongo open-source document database and the open-source official C# driver.
TestMongo
_ReSharper.TestMongo
AspFileDataCache.dat
RecentItems
RecentFiles.dat
TestMongo.suo
TestMongo
Classes
Demos
Images
mars.png
Thumbs.db
Methods
obj
x86
Debug
Properties
MongoDBDemoAsync
.vs
MongoDBDemoAsync
v15
.suo
MongoDBDemoAsync.sln.DotSettings.user
MongoDBDemoAsync
bin
Debug
Release
Classes
Demos
Images
mars996.png
Interfaces
MongoDBDemoAsync.csproj.DotSettings
obj
Properties
packages
´╗┐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

About the Author

George Swan
Student
Wales Wales
No Biography provided

You may also be interested in...

Pro
Pro
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.180621.3 | Last Updated 20 Nov 2017
Article Copyright 2013 by George Swan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid