Databases : understand how to organize and to use data

NoSQL Document-Oriented Databases : Introduction with MongoDB

A review of what you already know about NoSQL DB


Differences between Relational Databases and NoSQL

Aspect Relational Databases NoSQL Databases
Structure Data are stored in tables, a table is composed of a set of columns (structure), on which you can give constraints (control). Each row represents an entity instance, modifying a table has a high impact on the application Data are stored in collections of documents, graphs, or key-value pairs, it depends on the application which uses the database
Schema Once the schema is defined, the RDB can achieve good integrity on data, it uses constraints to control this integrity. This approach makes the development more robust, but allows less flexibility There is no schema by default, even if you can add one later to reinforce integrity checks
Duplication One of the main goals of relational DB is to avoid data duplication, at the price of heavy modeling and sometimes huge efforts to achieve performant queries NoSQL databases try to focus on speed for data retrieval and duplication is then encouraged. This requires more checks on the application side

Documents and collections

What's a document? A Json "property:value" set of values?

{
     "firstFieldName": firstFieldValue,
     "secondFieldName": secondFieldValue,
        ...
     "anotherField": fieldValue
}

Documents properties values can naturally be nested documents, and can contain list, which is very uncommon in relational databases

This will allow to create data really easily, but will have a high impact on how things will be checked on application side

Mongo DB: Hands-on

Mongo DB: Hands-on (2)

What Happened?

Some useful commands


show collections    //will show all the available collections
db.cities.find()    //will display the whole content of the collection
db.help()           //displays all the commands
db.cities.help()    //will display all the commands related to the collection
    

Mongo DB: Hands-on (3)

JavaScript is the "natural" language of mongo

What Happens?

With mongodb, you can script directly from the console, and customize the way you interact with the db

Mongo DB: Customizing and scripting

Exercise: write a javascript function to automate the insertion of the city seen at slide 4

function insertCity(
name
        //TODO complete arguments list
) {
db.cities.insert({
    city: name,
    //TODO : complete here
});

then copy-paste it in a js file (aside of the mongo executable) and load it through the command "load()" then try to invoke it on a new city

Mongo DB: import file from Mongo DB compass

import from here : cities

Mongo DB: Searching

Mongo DB: Updating (exercise)

Problem with city population : how to convert?

using function: write a js function that will use the parseInt function to convert the string value into an int

you can use the find().forEach(), and the collection.save()

solution:
db.cities.find().forEach( function (d) {
        d.population= parseInt(d.population);
        db.cities.save(d);
        });

Mongo DB: Using compass

Compass is the admin tool of Mongo

It helps by offering a UI to provide easy administration of databases and collections

it can help building pipelines for complex queries

it has schema analysis tools for your collections

Mongo DB: Using compass for "simple" querying

Try to build queries from the operator here