Simple-MongoDB – Alpha version is out
2010/03/02 2 Comments
Ok, it’s finally out. My driver agains MongoDB can be downloaded from: http://code.google.com/p/simple-mongodb/
I will try to write more about how to use it, in the next couple of days, but for now I will just double post the info that’s posted on the Google code site.
A C# implementation of a driver that can be used to communicate with MongoDB. The focus lies in keeping it “dynamic” by using anonymous types and JSON.
Using a well known JSON/BSON-serializer (http://james.newtonking.com/projects/json-net.aspx).
Roadmap
API
The API comes in two flavours: one where you directly consume commandobjects (insert, update, delete, query);and one where this is abstracted away, where you instead use a session with hierarchies.
Some simple examples of how the API currently looks like (it’s an alpha release so the API will probably have some changes).
Connection
To work with Simple-MongoDb you need to establish a connection to a MongoDb-server. This is done by creating an instance of SimoConnection (simo = Simple-MongoDb). The SimoConnection needs to know: address and port of the server. This is wrapped in SimoConnectionInfo, which could be populated manually or via connectionstring-name:
var cn = new SimoConnection(new SimoConnectionInfo(Constants.ConnectionStringName));
<connectionStrings> <add name="SimpleMongoDbTests" connectionString="host:localhost;port:27017"/> </connectionStrings>
Below I have wrapped this in a factory-method CreateConnection().
Commands
var document = new { Name = "Daniel", Age = 29 };
using (var cn = CreateConnection())
{
var insertCommand = new InsertDocumentsCommand(cn)
{
FullCollectionName = "TestDb.Persons",
Documents = new[] { document }
};
insertCommand.Execute();
}
Session
var document = new { Name = "Daniel", Age = 29 };
using (var session = new SimoSession(CreateConnection()))
{
session["TestDb"]["Persons"].Insert(document);
}
Querying could be done in a numerous ways(not yet regexs, it’s on the roadmap).
var query = @"{Age : 29}"var query = @"{$where: ""this.Name == 'Daniel' && this.Age == 29""}"var query = new {Name = "Daniel"}var query = new Person {Name = "Daniel"}
using (var session = new SimoSession(CreateConnection()))
{
var persons = session[DbName][PersonsCollectionName]
.Find<Person>(new WhereOperator("this.Age > 20 && this.Age < 65"));
}
//Daniel

What are the advantages of this over the existing community-sponsored and very active .NET driver for MongoDB? (http://github.com/samus/mongodb-csharp)
My project is a result of using Sam Corder’s driver (the one that you mention). It’s a great product. The things I wanted to change was to remove the tight coupling to a single class like Document (which was something I did if you check at my earlier writings). I want to be able to use anonymous types, static types as well as json without having to inherit or extend some certain class. I also wanted to make use of the existing Json/Bson.Net library for dealing with BSON and JSON.
//Daniel