Source Code - http://mongomvc.codeplex.com
In this post, I will give an introduction to how to work on NoSQL and document database with MongoDB , NoRM and ASP.Net MVC 2.
NoSQL and Document Database
The NoSQL movement is getting big attention in this year and people are widely talking about document databases and NoSQL along with web application scalability. According to Wikipedia, "NoSQL is a movement promoting a loosely defined class of non-relational data stores that break with a long history of relational databases. These data stores may not require fixed table schemas, usually avoid join operations and typically scale horizontally. Academics and papers typically refer to these databases as structured storage".
Document databases are schema free so that you can focus on the problem domain and don't have to worry about updating the schema when your domain is evolving. This enables truly a domain driven development. One key pain point of relational database is the synchronization of database schema with your domain entities when your domain is evolving.There are lots of NoSQL implementations are available and both CouchDB and MongoDB got my attention. While evaluating both CouchDB and MongoDB, I found that CouchDB can’t perform dynamic queries and later I picked MongoDB over CouchDB. There are many .Net drivers available for MongoDB document database.
MongoDB is an open source, scalable, high-performance, schema-free, document-oriented database written in the C++ programming language. It has been developed since October 2007 by 10gen. MongoDB stores your data as binary JSON (BSON) format . MongoDB has been getting a lot of attention and you can see the the list of production deployments from here
NoRM – C# driver for MongoDB
NoRM is a C# driver for MongoDB with LINQ support. NoRM project is available on Github at http://github.com/atheken/NoRM.
Demo with ASP.NET MVC
I will show a simple demo with MongoDB, NoRM and ASP.NET MVC. To work with MongoDB and NoRM, do the following steps
- Download the MongoDB databse For Windows 32 bit, download from http://downloads.mongodb.org/win32/mongodb-win32-i386-1.4.1.zip and for Windows 64 bit, download from http://downloads.mongodb.org/win32/mongodb-win32-x86_64-1.4.1.zip . The zip contains the mongod.exe for run the server and mongo.exe for the client
- Download the NorM driver for MongoDB at http://github.com/atheken/NoRM
- Create a directory call C:\data\db. This is the default location of MongoDB database. You can override the behavior.
- Run mongod.exe. This will start the MongoDb server.
Now I am going to demonstrate how to program with MongoDb and NoRM in an ASP.NET MVC application.
Let’s write a domain class
ObjectId is a NoRM type that represents a MongoDB ObjectId. NoRM will automatically update the Id becasue it is decorated by
the MongoIdentifier attribute. The next step is to create a mongosession class. This will do the all interactions and persistance to the MongoDB using the NoRM.
The MongoSession constrcutor will create an instance of MongoQueryProvider that supports the LINQ expression and also create a database with name "Expense". If database is exists, it will use existing database, otherwise it will create a new databse with name "Expense". The Save method can be used for both Insert and Update operations. If the object is new one, it will create a new record and otherwise it will update the document with given ObjectId.
Let’s create ASP.NET MVC controller and controller actions for handling CRUD operations for the domain class Category
You can easily work on MongoDB with NoRM and can use with ASP.NET MVC applications. I have created a repository on CodePlex at http://mongomvc.codeplex.com where you can download source code of the ASP.NET MVC application.