C# driver for MongoDB

I have started to build my own driver against MongoDb, where I’m using Newtonsofts-Json-library for dealing with JSON and BSON serialization.

It will be open source.

The first draft of how you can insert documents looks like this:

[Serializable]
public class Person
    : MongoDocument
{
    public virtual string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        //Example 1 - Using Connection and manually configured default ConnectionInfo
        using (var cn = new MongoConnection(new ConnectionInfo("localhost", 27017)))
        {
            var cmd = new InsertDocumentsCommand(cn);
            cmd.FullCollectionName = "MyDb1.MyCol1";
            cmd.Documents = new[] { new Person { Name = "Daniel" } };
            cmd.Execute();
        }

        //Example 2 - Using Session, with DefaultConnection.
        using (var session = new MongoSession(new MongoConnection(ConnectionInfo.Default)))
        {
            session["MyDb2"]["MyCol2"].Insert(new Person { Name = "Daniel" });
        }

        //Example 3 - Using Session, using connectionstring-settings from app.config
        using (var session = new MongoSession(new MongoConnection(new ConnectionInfo("MongoDBServer"))))
        {
            session["MyDb3"]["MyCol3"].Insert(new Person { Name = "Daniel" });
        }
        //var dynamic = new DynamicDocument(@"{Name:""Daniel""}");
    }
}

The MongoDocument that is being extended in Person has no komplexity at all. It just provides the MongoDB _id-bag.

[Serializable]
public abstract class MongoDocument
    : IMongoDocument
{
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string _id { get; set; }
}

I will include the functionality from my earlier writings on this topic, to deal with dynamic-documents (JSON).

//Daniel

2 thoughts on “C# driver for MongoDB

    • I was counting on you as a contributor 😉

      I’m planning of getting the basics out this weekend. Currently at work, though….

      //Daniel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s