C# driver for MongoDB
2010/02/19 2 Comments
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

I’m all for giving this a try! When you have something up let us know
I was counting on you as a contributor
I’m planning of getting the basics out this weekend. Currently at work, though….
//Daniel