Click here to Skip to main content
15,886,095 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.3K   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 System.Text.RegularExpressions;

    using MongoDB.Driver;
    using MongoDB.Driver.Linq;

    public partial class Program
    {
       public static void DemoLinq(MongoCollection<ClubMember> collection)
       {
           var names =
              collection.AsQueryable().Where(p => p.Lastname.StartsWith("R") && p.Forename.EndsWith("an")).OrderBy(
                  p => p.Lastname).ThenBy(p => p.Forename).Select(p => new { p.Forename, p.Lastname });

           Console.WriteLine("People whos Lastname starts with 'R' and Forename ends with 'an'");
           foreach (var name in names)
           {
               Console.WriteLine(name.Lastname + " " + name.Forename);
           }

           var regex = new Regex("ar");
           Console.WriteLine("List of Lastnames containing the substring 'ar'");
           IQueryable<string> regexquery =
               collection.AsQueryable().Where(py => regex.IsMatch(py.Lastname)).Select(p => p.Lastname).Distinct();

           foreach (string name in regexquery)
           {
               Console.WriteLine(name);
           }



       }

    }

}

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