NoSQL Document-Oriented Databases : Introduction with MongoDB
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 |
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
use sampleDB
db.cities.insertOne({
"city": "Nice",
"lat": "43.7034",
"lng": "7.2663",
"country": "France",
"iso2": "FR",
"admin_name": "Provence-Alpes-Côte d’Azur",
"capital": "minor",
"population": "1006402",
"population_proper": "340017"
})
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
JavaScript is the "natural" language of mongo
db.cities.find
What Happens?
With mongodb, you can script directly from the console, and customize the way you interact with the db
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
import from here : cities
db.cities.find()
methoddb.cities.find({city:"Paris"})
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()
db.cities.find().forEach( function (d) {
d.population= parseInt(d.population);
db.cities.save(d);
});
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
Try to build queries from the operator here