Skip to content
hugoware edited this page Sep 14, 2010 · 7 revisions

CSMongo is a driver written in C# for working with a Mongo database server for the .NET languages. CSMongo works hard to bridge the dynamic abilities of Mongo with the static restrictions of C#.

Here are some examples of how you can use CSMongo to work with a Mongo database.


//create a database instance
using (MongoDatabase database = new MongoDatabase(connectionString)) {

    //create a new document to add
    MongoDocument document = new MongoDocument(new {
        name = "Hugo",
        age = 30,
        admin = false
    });

    //makes changes to a document by overwriting
    //properties that exist or creating new
    //sub properties entirely
    document += new {
        admin = true,
        website = "http://www.hugoware.net",
        settings = new {
            color = "orange",
            highlight = "yellow",
            background = "abstract.jpg"
        }
    };

    //add and assign new values
    document["username"] = "hugoware";
    document["languages.primary"] = "english";
    document.Set("location", new {
        city = "Abilene",
        state = "Texas"
    });

    //remove properties from the object
    document -= "languages";
    document -= new[] { "website", "settings.highlight" };

    //or even attach other documents
    MongoDocument stuff = new MongoDocument(new {
        computers = new [] { 
            "Dell XPS", 
            "Sony VAIO", 
            "Macbook Pro" 
            }
        });
    document += stuff;

    //insert the document immediately
    database.Insert("users", document);

}

A MongoDocument allows for a fairly dynamic experience even inside of a static language like C#. The next example shows how you can access values after loading them from the database.


//create the database instance
using (MongoDatabase database = new MongoDatabase(connectionString)) {

    //creates a query using a series of chained methods
    //to define the requirements (assumes we find the 
    //document we just entered in the previous example)
    var document = database.Find("users")
        .EqualTo("admin", true)
        .Greater("age", 20)
        .SelectOne();

    //work with the properties one at a time
    string name = document.Get<string>("name"); // "Hugo"
    int age = document.Get<int>("age"); // 30
    string color = document.Get<string>("settings.color"); // "orange"

    //you can set defaults for values incase they cannot
    //be converted or they are missing
    string website = document.Get<string>("website", "missing"); // "missing"
    string missing = document.Get<int>("not.a.real.property", 4); // 4

    //Maps a document to an anonymous type
    //Each property on the created type is the default
    //value if nothing is found in the document
    var user = document.Get(new {
        name = "undefined",
        age = -1,
        settings = new {
            background = "missing.jpg",
            font = "Monospace"
        }
    });

    //then use the values normally
    Console.WriteLine(user.age); // 30
    Console.WriteLine(user.settings.background); // "abstract.jpg"
    Console.WriteLine(user.settings.font); // "Monospace"

}
Clone this wiki locally