Click here to Skip to main content
15,892,005 members
Articles / Database Development / NoSQL

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

Rate me:
Please Sign up or sign in to vote.
4.92/5 (43 votes)
16 Sep 2018CPOL13 min read 199.8K   3.4K   101  
Highlights the latest developments in both the Mongo open-source document database and the open-source official C# driver.
namespace MongoDBDemoAsync
{
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;

    using MongoDB.Driver;

    public class DemoManager
    {
        #region Constants and Fields

        private readonly IMongoCollection<ClubMember> collection;
        private readonly Dictionary<int, Tuple<IDemo, string>> DemoDictionary;
        #endregion

        #region Constructors and Destructors

        public DemoManager(IMongoCollection<ClubMember> collection)
        {
            this.collection = collection;
            DemoDictionary = new Dictionary<int, Tuple<IDemo,string>>() {
                { 1,Tuple.Create<IDemo,string>(new AggregationDemo(),"Aggregation Demo") },
                { 2,Tuple.Create<IDemo,string>(new GridFSDemo(),"GridFS Demo") },
                { 3,Tuple.Create<IDemo,string>(new LinqDemo(),"Linq Demo") },
                { 4,Tuple.Create<IDemo,string>(new MapreduceDemo(),"Map Reduce Demo") }
            };
        }

        #endregion

        #region Public Methods and Operators

        public async Task<bool> PromptNextDemoAsync()
        {
            Console.WriteLine("Menu");
            foreach (KeyValuePair<int,Tuple<IDemo, string>> entry in DemoDictionary)
            {
                Console.WriteLine($"({entry.Key}) {entry.Value.Item2} ");
            }
            Console.WriteLine($"({DemoDictionary.Count+1}) Exit ");
            int choice=   ConsoleHelper.GetNumberFromUser("Enter Menu choice",1,DemoDictionary.Count+1);
            if (choice != DemoDictionary.Count + 1)
            {
               await DemoDictionary[choice].Item1.RunDemoAsync(collection);
                return true;
            }
            return false;
         
        }

        #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)


Written By
Student
Wales Wales
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions