Experiments with MongoDb
During the Xmas holidays I found the time to have a look at MongoDb a Document oriented Database where the schema is based on JSON.
I downloaded the binaries and the C# Driver and I started some experiments.
The setup-time is minimal, the server is a console application that remain ready on a certain port:
D:\mongodb-win32-x86_64-1.2.1\bin>mongod.exe --dbpath ./dataTue Jan 05 19:16:20 Mongo DB : starting : pid = 0 port = 27017 dbpath = ./data master = 0 slave = 0 64-bitTue Jan 05 19:16:20 db version v1.2.1, pdfile version 4.5Tue Jan 05 19:16:20 git version: 45992de574979343f34fdfe96b069d5d1eff0182Tue Jan 05 19:16:20 sys info: windows (6, 0, 6002, 2, 'Service Pack 2') BOOST_LIB_VERSION=1_39Tue Jan 05 19:16:20 waiting for connections on port 27017
Above I simply specified the data folder even if there are a lot of parameters you can set.
The first sample that I write is this:
Mongo mongo = new Mongo();mongo.Connect();Database db = mongo.getDB("MyTestDb");IMongoCollection posts = db.GetCollection("Persons");Document doc = new Document();doc["Name"] = "Emanele";doc["Surname"] = "DelBono";posts.Insert(doc);Document example = new Document();example["Name"] = "Emanuele";ICursor cursor = posts.Find(example);foreach (Document document in cursor.Documents){Console.WriteLine(document.ToString());}
The first block is needed to open the “connection” to the database and to select with collection to work with.
Then I create a new Document and I fill in the key value pairs. The Document class is a dictionary that keep the document data and in JSON format and is converted BSON by the driver when the insert instruction get called.
The third block is a query to the database. I created a new document with a property name and the needed value, the Find method get a cursor from the database with all the matching document (i.e. all the persons whose name is Emanuele).
The power is in the storage and query method. You can create a complex document (with sub-documents, collection and so on) and store like a simple document.
This is a very first basic example on how to setup and run a micro-application on MongoDb, I’m now working on an extension to transform domain model objects in Document hierarchies so I can avoid the manual mapping from a Person class to the Document class.
Maybe in a next post I’ll talk more about that.
Posted in Emanuele DelBono | 6 comments6 Comments so far
Leave a reply
RSS



Social comments and analytics for this post…
This post was mentioned on Twitter by emadb: [blog post]: Experiments with MongoDb http://tinyurl.com/ybk253g #NoSQL #MongoDb…
This mongoDB thing is super interesting. I’ve started playing around with it, and it looks very promising for a lot of domains.
I thought about making my blog run on this, but it’s on a VPS with low memory, and I think that the whole idea of mongo is to try and keep as much in memory as possible.
V…
V…
have you ever tried “Windows Azure Storage Services / Blob Service API” ?
http://msdn.microsoft.com/en-us/library/dd135733.aspx
@Simone
Not yet, but they are in my TODO List
Most significantly, we have spend lot of time using a database. Thought a series of experiment and mini-projects, you will come away comfortable using MongoDB and well-prepared to start building application with it.