Click here to Skip to main content
15,895,011 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.1K   3.4K   101  
Highlights the latest developments in both the Mongo open-source document database and the open-source official C# driver.
using System;
using System.Linq;

namespace TestMongo
{
    using MongoDB.Driver;
    using MongoDB.Driver.Builders;
    using MongoDB.Driver.Linq;

    public partial class Program
    {
        public static void DemoQueryBuilder( MongoServer server,  MongoDatabase database,MongoCollection<ClubMember> collection)
        {
            DateTime membershipDate = DateTime.Now.AddYears(-5);

            //DateTime is stored in BsonElement as a UTC value so need to convert
            var recentMembers = collection.Find(Query.GT("MembershipDate", membershipDate.ToUniversalTime()));
            Console.WriteLine("Members who have joined in the last 5 years ...");
            foreach (ClubMember clubMember in recentMembers)
            {
                clubMember.PrintDetailsToScreen();
            }
            //This 'Find' should be efficient as it will use the index 'myIndex'
            //For finding and sorting
            var jonesRees =
                collection.Find(Query.Or(Query.EQ("Lastname", "Rees"), Query.EQ("Lastname", "Jones"))).SetSortOrder(
                    SortBy.Ascending("Lastname", "Forename").Descending("Age"));
            Console.WriteLine("Members named Jones or Rees ...");
            foreach (ClubMember clubMember in jonesRees)
            {
                clubMember.PrintDetailsToScreen();
            }
            int result2 =
                collection.AsQueryable().Select(c => c).Count(c => c.Lastname == "Jones" && c.Forename == "David");
            Console.WriteLine("The number of members named David Jones is {0}", result2);

            //The following 'using' statement makes sure all database traffic is  on the same thread
            // This is necessary because the newly written data is being queried straight away.
            // This can give rise to concurrency problems if different threads are used
            using (server.RequestStart(database))
            {
                //Change the name of every David Jones to Dai Jones
                IMongoQuery davidJonesQuery = Query.And(Query.EQ("Lastname", "Jones"), Query.EQ("Forename", "David"));
                UpdateBuilder update = Update.Set("Forename", "Dai");
                collection.Update(davidJonesQuery, update, UpdateFlags.Multi);

                IQueryable<ClubMember> result3 =
                    collection.AsQueryable().Where(c => (c.Lastname == "Jones" && c.Forename == "Dai"));
                foreach (ClubMember name in result3)
                {
                    name.PrintDetailsToScreen();
                }
            }

        }
    }
}

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