Click here to Skip to main content
15,352,484 members
Articles / Database Development / MongoDB
Article
Posted 19 Feb 2016

Tagged as

Stats

15K views
5 bookmarked

Access Layer light and fast data: MongoDB / .NET

Rate me:
Please Sign up or sign in to vote.
3.75/5 (6 votes)
19 Feb 2016CPOL6 min read
MongoDB, everyone talks about it, here article guides you step in its integration with Microsoft.NET.

Introducing MongoDB

What is MongoDB?

MongoDB is a database-oriented paper sponsored by 10gen. It is used especially in Foursquare, SourceForge and bit.ly.

Mongo stores documents in BSON formats (binary JSON supports a little more value types) and provides a shell javascript to access data and make administrative operations. Many drivers are implemented including Java, .Net, PHP, Javascript. The basic concept oriented document means that the stored objects are represented in the form of a document BSON (not only a key style map / value) easily to be mapped to the objects we manipulate in our programs. You could compare it to a storage of an XML representation of a bunch of objects.

Schemaless: Comparison MongoDB NoSQL and traditional relational databases

When developing a data access layer, we often have to either use an ORM, or use a map that will have developed ourselves, but both will do the same job that is processing and related object mapping.

More like conventional relational databases do not allow the storage of complex data (entity with tables, nesting, collection), we have to perform various SQL queries to obtain our data or update.

In view of these two points, the development of a layer of data access a lot of work. These days we are losing to implement technical and non functional answer, we could affect the business development and satisfy our customer advantage.

For this one solution, store .NET objects as they are and ask our persistence engine for us to manage this work. A dream that MongoDB and NoSQL databases allow to hold. It's called the schemaless.

What does the sharding with MongoDB?

MongoDB since version 1.2 incorporates a sharding management. The implementation of this management allows for greater scalability. Our data are all consistent, because it always accesses all. The concept of sharding is not to have multiple databases in master / master, but one database which distributed its own data in various forums. We meet and to have some data on an instance and the other another. This aspect is not detailed in this article.

What does the GridFS?

MongoDB embarks with him a complete file system. This system is GridFS. MongoDB is limited to 4MB per document, but we quickly need to store important documents. GridFS allows it. So we store any document greater than 4 MB of GridFS file system. GridFS and allows for the sharding on its files. So we need rsync between machines. GridFS did the job for himself. One could easily bring in GridFS Hbase.

An advanced query system

Finally the true interest of MongoDB in my opinion, especially compared to Riak or CouchDB is its Query Management. of how it can be queried very fine thanks to a large amount of keywords. We can make as rich as in SQL queries, but all on a database-oriented documents.
One can for example search all documents that have multiple elements in an array
db.things.find ({a: {$ all: [2, 3]}});
While in SQL would have made a more
select * from things WHERE a LIKE '% 2%' AND has LIKE '% 3%'
But it is incomplete because eventually it will work for a "To" field containing an array [32] SQL but will not work in the case of the MongoDB query.
We can also do research by giving
Javascript db.myCollection.find ({$ where "this.a> 3"});
It is highly modular and the number of keywords is increasing.

Get started in .NET 4.5

There are many utility library to communicate with MongoDB, I advise you to stay and use the standard driver provided by the same MongoDB project.

Architecture MongoDB layer that will be implemented

Image 1

Set the layer

We will use the classic mechanism of the other DAL. Namely The connectionstring in the web.config to set the server

 < connectionStrings >
    < add name="MongoDB" connectionString="mongodb: // localhost" > < / add >
< / connectionStrings >

The name of the database application setting

 < appSettings >
...
< add key="MongoDBName" value="MyTvProgram" / >
< / appSettings >

MongoDB installation on your machine (single instance)

  1. Before starting the installation, you must download the latest version of MongoDB. To do this, go to the following address: http://www.mongodb.org/downloads and choose the type of installation you want.
  2. To start the installation, simply double-click the msi package and follow the instructions on the screen
  3. Create a folder to hold your databases c:\data\db and a log file c:\data\log
  4. Create a configuration file that allows you to configure mongodb: mongod.conf
  5. You can start using the mongod mongod.exe program located in the bin folder: config mongod mongod.conf
  6. You can also configure mongod to use as a service: mongod mongod.conf config --install
  7. Configure the mongod starts up automatically or manually: sc config mongod start=auto

Hosting a MongoDB database with replication on Windows Azure

Windows Azure natively supports MongoDB do not, you have the example mongolab SAAS services, but it is limited, so you can support a free MongoDB cluster, you must create two basic VM

  1. Create a VM name the < yourcompany > mgdb1
  2. Create another VM name the < yourcompany > mgdb2
  3. Assign a drive to each of your VM 30 Gb and associate the letter f

Repeat the following operation on each VM

  1. Log into your first machine
  2. Format your disk data
  3. Disable IE control.
  4. Add http: //*.mongodb.org to the trusted sites list
  5. Download the version 64bits
  6. Install MSI
  7. MongoDB default is installed in the C:\Program Files\MongoDB\Server\3.2\bin, create a Path environment variable pointing to the file
  8. Download and install the OpenSSL, this will allow you to create a certificate for your replication: https://slproweb.com/download/Win64OpenSSL_Light-1_0_2f.exe
  9. Create 2 folders on your drive f: data and log
  10. Create 1 conf folder
  11. Add a mongod.conf file
  12. Add the necessary parameters:
    • Port: The port number on which the default listened 27017
    • dbPath: path data
    • replSet Rs0 =: indicates that a replication will use
    • logpath: indicates the path to the log file
    • logaappend indicates the rotation
  13. If the node is not the first, create the folder and copy the keys mongodb keyfiles-file of the first node.
  14. Install Service mongodb mongod --config f:\conf\mongod.conf --install
  15. Start the net mongdb Service start mongodb
  16. If this is the first node, log in to the server and start the mongo rs.initiate replication ()
  17. Open a port in the firewall in order to connect to the service MongoDB
  18. Configure an endpoint in Windows Azure for the virtual machine on TCP port 27017
  19. If this is the first node, create administrative users on the master
    1. use admin
    2. db.createUser ({
    3. user: "siteUserAdmin"
    4. pwd: "< password >"
    5. Actual: [{role: "userAdminAnyDatabase" db "admin"}]
    6.});
    7. db.createUser ({
    8. user: "siteRootAdmin"
    9. pwd: "< password >"
    10. Actual: [{role: "root", db: "admin"}]
    11.});
  20. Stop the instance of mongodb
  21. Create the first node only a key file to be used by each member of a replication
    • I recommend using openssl but you can use other mechanism.
    • Copy the key file generated on each member
    • Edit the configuration file to specify the key file to use. Keyfile
    • Repeat the operation on the first server

Add a node to a replication

  1. From the host machine of the new node,
  2. Connect to the first node mongo <premiernoeud> -u siteRootAdmin -p <password> --authenticationDatabase admin
  3. Use the rs.add method ( "nomdelamachine: 27017") to add the node

Check the configuration of replication.

  1. From any host
  2. Connect to the first node mongo <premiernoeud> -u siteRootAdmin -p <password> --authenticationDatabase admin
  3. Use the rs.conf () method

Development of the access layer data in Visual Studio 2015

  1. Repeat libraries previously created in our section 1 and 2,
  2. In the STPVideoPlayer.DataLayer project, add the following classes
    • EpisodeModel: to describe an episode
    • LocalVideoModel: To describe a local video file
    • YoutubeModel: To describe a video or playlist hosted on youtube
    • TimeLineModel: To describe a planning
  3. Install MongoDB driver for the NuGet STPVideoPlayer.Data.MongoProvider project

    Image 2

  4. Create a class TvPlayerRepository
  5. Implement the code to our provider by implementing the interface ITvPlayerRepository

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

mbruchet
Architect
France France
Hy, I'm comming success on Microsoft Certification profesionnal 70-480 on HTML 5 / CSS 3 / JavaScript and I preparing the next certificate 70-486. This certificat is required to MCSD Web And Azure.

I'm a Asp.NET Expert, I can help you to develop your web site and i can host it on Microsoft Azure Data center.

My budget is depend on your functions.

I can talk you tomorrow if you want to exchange about it.

Thanks

Comments and Discussions

 
QuestionMongoDB Document limit Pin
Member 968618422-Feb-16 10:48
MemberMember 968618422-Feb-16 10:48 
GeneralGood Work Pin
Alireza_136221-Feb-16 9:40
MemberAlireza_136221-Feb-16 9:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.