Introduction:
As we said in the description, we will start with the Model part and especially with the well known document based database (NoSQL) MongoDB.
If you are familiar with JSON you will rapidly notice the similarity between the two, it’s (key : value) based, the values may contain a document, arrays, and arrays of documents the thing that can let you far from complex join relations that are also expensive in what we call performance …
A MongoDB server can contain multiple databases and each database contains multiple collections that can be in relations (OneToOne, OneToMany, ManyToOne and ManyToMany).
An Example of a MongoDB collection :
1 | [ |
Installation:
First of all you need to install a MongoDB locally, here is a link in the official documentation that contains all the steps for all environnements :
Now that we have MongoDB installed locally, we will install a tool that will help us to gather the data from a specific database in our server.
The tool is called Mongoose, it’s the linker between the web server and the model server.
It’s kind usefull and contain many features that will help us a lot, like tha callbacks features.
Mongoose installation using npm :
1 | npm install -g mongoose |
We will play around just to get used to it, first we type the following command:
1 | node |
Now we will declare a mongoose variable that contains the Mongoose module that also contains all the functions we need .
You should know that nodejs has a module loading systems, if you want to add functions or objects to your root module you should add them to module.exports
Types of modules :
- Core modules like the ‘http’ module, they are contained in the /lib folder and are compiled to binary.
- File module, files that end with .json , .js and finally .node .
1 | var mongoose = require("mongoose"); |
Then we connect it to the installed database :
1 | var connectionCallBack = function(err){ |
Before creating our first Document we should design its Schema first, so we should create a Schema object :
1 | var Schema = mongoose.Schema; |
We will create a User Schema :
1 | var userSchema = new Schema({ |
So our schema contains three properties : name, age and address .
The address is an Embedded Sub-document that contains : street and city, we did that because the little amount of data that can contain an address, if for example
we had a relation user<=>posts we shall not do this, we will store the posts in a distinct documents and we will set a ManyToOne relation.
An “_id” will be added automatically as a primary key with unique value, its type is ObjectId a type contained in “mongoose.Schema.Types.ObjectId”.
Now that our Schema is ready, all that we have to do is to add it to the database.
1 | /*in the first parameter we set the name of the document and in the the second one the the Schema we created*/ |
CRUD Operations :
Create :
1 | var userCallBack = function(err, user) |
Read :
1 | var usersCallBack = function(err, users) |
Update :
1 | var usersCallBack = function(err, users) |
Delete :
1 | var deletionCallBack = function(err) |
You should take a look here for more informations ;) .
That’s it for the Model Part :D