MongoDb data access with .NET

Not the usual choice for .NET development but NoSql document databases are becoming more common in the development world.


Getting MongoDb running on Windows

  1. To get started with MongoDb first download the correct version for your operating system.
  2. Create the following new path c:datadb
  3. From the extracted files, run MongoD.exe.
  4. That’s it.
mongodbrunning

.NET with MongoDb

To communicate with MongoDb you need to first download the assembles from github or do it the smart way and use Nuget.  With this C# driver you can now query the database directly using LINQ statements:

There are a few different ways you can construct  the data model when using C# with MongoDb such as using a BsonDocument from a JSON string or do it the “typed” way and use custom objects for your documents.

Note the BsonId Attribute, which is  needed to act as the document identifier.

public class Customer
{
BsonId]
public ObjectId CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime CreatedDate { get; set; }
public IEnumerable Orders { get; set; }
}

Data Access

With MongoDb you do not need to specifically create a new database or collection. When calling the save method, if the database is not there, it will be created, if that collection is not there, it will be created.

Insert

string mongoConn = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(mongoConn);
var myStore = server.GetDatabase("myStore");
var coll = myStore.GetCollection("myCustomer");
var cust = new MongoCustomer()
{
FirstName = "john",
LastName = "doe",
CreatedDate = DateTime.Now,
MongoOrders = GetSomeOrders()
};
coll.Save(cust);

(You can also use the Insert or InsertBatch methods in the MongoCollection Object.

Select

string mongoConn = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(mongoConn);
var myStore = server.GetDatabase("myStore");
var coll = myStore.GetCollection("myCustomer");
var q = from c in coll.AsQueryable()
where c.FirstName.Contains("russ")
select c;

Performance Testing

Our simple data model includes a one to many relationship of a Customers to Orders.

MongoDb vs SQL Server 2008 Express

Insert

mongodb_vs_sqlserver_insert

As you can see, inserting data is faster in MongoDb, especially when the data model gets more complex.

Select

The loaded data for selection testing includes 3,000,000 customers and 10 orders per customer in both sql server and MongoDb.
mongodb_vs_sqlserver_select

With a large amount of data being returned MongoDb is much faster, but it seems the RDBMS had the advantage for smaller queries, at least with this data setup.

If the data structure and business need fall in line with the document database model then using MongoDb with .NET can make some very high performing applications.


2 thoughts on “MongoDb data access with .NET

  1. Pingback: Interesting .NET Links – June 20, 2012 | TechBlog

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>