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
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)
- 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.
- To start the installation, simply double-click the msi package and follow the instructions on the screen
- Create a folder to hold your databases c:\data\db and a log file c:\data\log
- Create a configuration file that allows you to configure mongodb: mongod.conf
- You can start using the mongod mongod.exe program located in the bin folder: config mongod mongod.conf
- You can also configure mongod to use as a service: mongod mongod.conf config --install
- 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
- Create a VM name the < yourcompany > mgdb1
- Create another VM name the < yourcompany > mgdb2
- Assign a drive to each of your VM 30 Gb and associate the letter f
Repeat the following operation on each VM
- Log into your first machine
- Format your disk data
- Disable IE control.
- Add http: //*.mongodb.org to the trusted sites list
- Download the version 64bits
- Install MSI
- MongoDB default is installed in the C:\Program Files\MongoDB\Server\3.2\bin, create a Path environment variable pointing to the file
- 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
- Create 2 folders on your drive f: data and log
- Create 1 conf folder
- Add a mongod.conf file
- 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
- If the node is not the first, create the folder and copy the keys mongodb keyfiles-file of the first node.
- Install Service mongodb mongod --config f:\conf\mongod.conf --install
- Start the net mongdb Service start mongodb
- If this is the first node, log in to the server and start the mongo rs.initiate replication ()
- Open a port in the firewall in order to connect to the service MongoDB
- Configure an endpoint in Windows Azure for the virtual machine on TCP port 27017
- 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.});
- Stop the instance of mongodb
- 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
- From the host machine of the new node,
- Connect to the first node mongo <premiernoeud> -u siteRootAdmin -p <password> --authenticationDatabase admin
- Use the rs.add method ( "nomdelamachine: 27017") to add the node
Check the configuration of replication.
- From any host
- Connect to the first node mongo <premiernoeud> -u siteRootAdmin -p <password> --authenticationDatabase admin
- Use the rs.conf () method
Development of the access layer data in Visual Studio 2015
- Repeat libraries previously created in our section 1 and 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
- Install MongoDB driver for the NuGet STPVideoPlayer.Data.MongoProvider project
- Create a class TvPlayerRepository
- Implement the code to our provider by implementing the interface ITvPlayerRepository