NoSQL

Things to know when switching from a RDMS to MongoDB

Before switching from a RDMS such as Oracle or SQL Server to MongoDB, one should be familiar with some key concepts of the NoSQL DataBase.

Translation needed

Before starting, we have should translate some traditional concepts of the RDMS world :

  • A collection is like a table
  • A document is a like a row
  • A column is a like a field

That said we can take a look at the key concepts of MongoDB.

Key Concepts

Dynamic schema

The fields of a document can be changed at anytime. Documents of a collection may have different fields or even different data types for the same field.

Atomicity of write operations

Write operations are atomic for one single document. A document will never be partially update, even if the document contains multiple sub-documents. On the contrary write operations on multiple documents are never atomic.

Unique Index

Unique index allows to have uniqueness. For example, we can create a unique index on the field person_id on the persons collection.

Read uncomitted

Readers can see the result of writes before the writes are said to be durable. A durable write is a write that persists after a shutdown or restart of one or more servers.

Replica

The same data set can be replicated in order to provide high availability (for read performance) or redundancy (for data security).

Sharding

A collection can be partitioned among multiple server  in order to offer horizontal scaling. Data is partitioning is based on a key called the shard key.

MongoDB is easy to start with but can be difficult to master. Those are the key concepts to know before diving more into the understanding of the NoSQL database.

NoSQL

A brief look at MongoDB

In this article, we are going to look at a NoSQL Database named MongoDB. MongoDB is a document oriented database that targets high performance and high volume. In this article, I am going to install a MongoD server on windows 10 64bits and use the C# client, but other operating systems for the server, as well as other languages for the client are avalaible.

From server installation to DataBase creation (Windows 10 64bits)

The first thing that we need is to download and install the .msi of the community version of MongoDB for Windows 64-bit. (I have personally downloaded the version 3.4.3).

Then we create a folder where the database will be installed in the disk. I have chosen to create a folder MongoDB in “C:\Data\”. But it could be anywhere else.

Third we need to start the server using mongod.exe :

> "C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --dbpath "C:\Data\MongoDB"

We can now connect to the server using  mongo.exe (on a new command window),

> "C:\Program Files\MongoDB\Server\3.4\bin\mongo.exe"

… and create a new database named testdb.

> use testdb

We have now a new database ready to be used!

Using the DataBase with C# client

Installing C# client via Nuget

The C# client can be downloaded from Nuget by installing the MongoDB.Driver package. I have personally installed it along a Console Application in .Net 4.6.2.

Creating documents

Remember that in NoSQL, we do not need define the structure of a collection before inserting the data. Here I am going to create two documents in the collection “persons“.

Finding a document

And now we are going to find one of them to see if the insertion went well. For that we are going to use a filter on the profile_id of a person.

Which works fine!

{ "_id" : ObjectId("58dfc1e6a515863c90cc3976"), "profile" : { "profile_id" : "1", "surname" : "Khakurel", "firstname" : "Pradip" } }

MongoDB is very intuitive and easy to use!!!