In this presentation we start by making a comparison between SQL (relational) and NoSQL DBMS. Next, we go through NoSQL DBMS types : column based, key-value based, document oriented and graph oriented dbms. MongoDB is one of the popular document oriented NoSQL DBMS.
In this presentation we discovered how data is structured in MongoDB between collections and databases. Then, we jumped to MongoDB queries, we identified all the possible queries : searching, inserting, updating and removing data queries.
MongoDB has a set of relevant indexes : text indexes, geographical indexes, unique indexes ...etc. Indexes are a data structures that help the DBMS administrator in accelerating his search queries.
At the end, we had an overview about Sharding and Replication in MongoDB.
4. NoSQL
Not Only SQL
SQL NoSQL
Schema-less.Predefined Schema
Redundancy.Normalization (No redundancy).
Scaling out (cluster-friendly).Scaling in.
ACID principles. No ACID principles.
No large data. Large data.
11. Start mongoDB session :
> mongo
Visualize your databases:
> show dbs
Create new database called « scolarity » :
> use schooling
12. Create a new collection named « students » :
> db.createCollection(“students”)
Visualize the content of your collection « students »:
> db.students.find()
Create new document on your collection « students » :
> db.students.insert({“first_name” : “yours”,
“last_name” : “yours”})
13. Visualize the content of your collection « students »:
> db.students.find().pretty()
Insert multiple documents at once in your collection « students » :
> db.students.insert([{“first_name” : “yours”,
“last_name” : “yours”},
{“first_name”: “Amine”, “last_name” : “Ali”, “age”
: 45}])
26. CRUD Operations
Insert a set of documents at once.
> var bulk = db.products.initializeUnorderedBulkOp();
> bulk.insert({“name”:””, “type”:””})
> bulk.insert ({“name”:””, “type”:””})
> bulk.execute ()
34. Explain plans & execution statistics
How do I examine the performance of my query ?
How can I know the indexes which have been used for my
query ?
> db.coll.explain().find({a:1}).sort({b:-1})
Fast scannability : to skip fields that aren’t relevant to the request
Data types : date data types (in Bson, not in JSON), BinData (like an image, UUID).
Object_ID :
JSON types : boolean, null, int, string, arrays, nested doc.
Speak about system databases.
Ordered = true (by default), all or nothing.
Ordered = false, it can insert few of them.
Write concern : add some constraintes to the request.
Full update, delete and insert
Partial Update : only modify.
without indexes : iterate over all the documents that appear in the collection
Btree indexes
Sparse : when we want to create an index over a field, which has a lot of null values
Geospecial indexes : for app who have some notions of Geospacial locality, supports 2 and 3 dimensional queries. For example, it can response to the query: the nearest coffee shop. Loc : [log, lat]
For example to search a word on a field that contains a phrase. We can define a text index over that field.
4 members, Replication factor = 4
Automatic failover :
If the primary server goes down, another server will be elected to become the new primary server
Writes are on the primary, reads may occure on secondaries = slaves .
Every node has a priority, the node with the highest priority will be the primary node.
ArbiterOnly : is a node dedicated to vote, it cannot vote for itself and it doesn’t have data on it.
If priority = 0, the node would never be primary.
Reasons behind replication : high availability and data safety (disaster recovery).