Click here to Skip to main content
13,634,016 members
Click here to Skip to main content
Articles » Database » NoSQL » General » Downloads

Tagged as

Stats

130.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
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)

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
Web02-2016 | 2.8.180712.1 | Last Updated 20 Nov 2017
Article Copyright 2013 by George Swan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid