Click here to Skip to main content
Click here to Skip to main content
Articles » Database » NoSQL » General » Downloads
 
Add your own
alternative version
Go to top

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

, 9 Jan 2013
Highlights the latest developments in both the Mongo open-source document database and the open-source official C# driver.
article.zip
article
Car.png
Demo.zip
Hadron.png
Demo-noexe.zip
TestMongo
_ReSharper.TestMongo
AspFileDataCache.dat
RecentItems
RecentFiles.dat
repSetDb0.cfg
repSetDb1.cfg
repSetDb2.cfg
TestMongo.suo
TestMongo
Classes
Demos
Images
mars.png
Thumbs.db
Methods
obj
x86
Debug
Properties
Demo.zip
AspFileDataCache.dat
RecentFiles.dat
repSetDb0.cfg
repSetDb1.cfg
repSetDb2.cfg
TestMongo.suo
bin
Debug
mars.png
Thumbs.db
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
TestMongo.csprojResolveAssemblyReference.cache
TestMongo.exe
TestMongo.pdb
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

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 9 Jan 2013
Article Copyright 2013 by George Swan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid