Click here to Skip to main content
15,896,475 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 200.3K   3.4K   101  
Highlights the latest developments in both the Mongo open-source document database and the open-source official C# driver.
namespace TestMongo
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using MongoDB.Driver;

    public partial class Program
    {
        #region Methods

        private static void BuildClubMembers(MongoCollection<ClubMember> collection, Random random, int counter)
        {
            string[] forenames = { "Anwen", "David", "Rhys", "Megan", "Sean", "Delyth", "Rhian", "Aled" };
            string[] lastnames = { "Jones", "Davies", "Williams", "Rees", "Parry", "Edwards", "Thomas", "Richards" };
            string[] vintageCars = { "Alvis", "Austin", "Cooper", "Bristol", "Hillman", "Humber", "Riley" };
            Random rand = random;
            for (int i = 0; i < counter; i++)
            {
                int forenameIndex = rand.Next(0, forenames.Count());
                int lastnameIndex = rand.Next(0, lastnames.Count());

                int totalNumberOfCars = rand.Next(0, vintageCars.Count());
                DateTime today = DateTime.Now;
                var member = new ClubMember
                {
                    Forename = forenames[forenameIndex],
                    Lastname = lastnames[lastnameIndex],
                    Age = rand.Next(16, 65),
                    MembershipDate = today.AddDays(-1 * rand.Next(0, 7200))
                };
                if (totalNumberOfCars > 0)
                {
                    member.Cars = new List<string>();
                }
                for (int c = 0; c < totalNumberOfCars; c++)
                {
                    int carIndex = rand.Next(0, vintageCars.Count());
                    member.Cars.Add(vintageCars[carIndex]);
                }

                collection.Insert(member);
            }
        }

        #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