by Ciro Donato Caiazzo
Bologna, July 21, 2015
MongoDB for
Developers
{
"Name" : "Ciro Donato",
"Surname": "Caiazzo",
"I am": "Software Engineer",
"Working in": "SCAI Consulting",
"With Title": "Senior Developer Consultant",
"ContactMethods" : [
{"Type": "Twitter", "Value": "@CyrusD87"},
{"Type": "Linkedin", "Value": "Ciro Donato Caiazzo"},
{"Type": "Personal e-mail", "Value": "cd.caiazzo@gmail.com"},
{"Type": "Job e-mail",
"Value": "ciro.caiazzo@scai-consulting.grupposcai.it"}
],
"Some passions": ["Tecnology","Innovation","Agile","Big data"]
}
What are NoSQL?
NoSQL developed in response to a rise in the
volume of data…
…with a focus on speed, performance, flexibility
and scalability…
… for the new modern applications.
 NoSQL data model addresses issues that
relational model cannot achieve (ex. It
manage Large volumes of structured, semi-
structured, and unstructured data, etc…)
 NoSQL databases are more scalable and
provide superior performance
NoSQL
DOCUMENT
GRAPH
KEY-VALUE
WIDE-
COLUMN
What is?
 MongoDB is a non relational datastore for
JSON documents
 Written in C++
 License GNU AGPL v3.0 (more..)
MongoDBBSON
{
"skills":{
"database":[
{
"name":"Sql",
"years":"7"
},
{
"name":"MongoDB",
"years":"2"
}
]
}
}
 MongoDB uses collections for storing groups
of data.
 A collection can be thought as a table in a
relational database.
MongoDB
Collection 2
Collection 3
Collection 1
RDBMS
Table 1
Table 2
Table 3
 Schemaless means that two documents that
don't have the same schema can be stored in
the same collection.
{"_id" : "1", "skill" : {"web": [{"name": "html"}, {"name": "css"}]}}
{"_id" : "2", "skill" : {"database": [{"name": "mongoDB"}, {"name": "sql
server"}]}}
{"_id" : "3", "skill" : {"methodologies": ["scrum","kanban","xp"]}}
MongoDB does not support:
 Joins;
 Transactions across multiple collections
But each operation is atomic at level of single
document
Reason?? Scalability!
In MongoDB, documents stored in a collection
require a unique _id field that acts as a primary key
ObjectId is a 12-byte BSON type, constructed
using:
 a 4-byte value representing the seconds since
the Unix epoch,
 a 3-byte machine identifier,
 a 2-byte process id, and
 a 3-byte counter, starting with a random value.
What does it happen for date after the 2038?
(Year 2038 problem)
On Unix systems, dates are represented as seconds
from 1 January 1970 and usually stored in the
C time_t type.
MongoDB does not use time_t to represent dates
internally so do not suffer from this problem
http://api.mongodb.org/python/2.1.1/faq.html
Where to start?
InWindows
 Download and install MongoDB
 https://www.mongodb.org/downloads
 Include MongoDB (bin) path in the
environment variable ‘PATH’
 Open Command Prompt and run the
following command to create the directories
where MongoDB will save the data:
Run ‘mongod’
And now? Go to the Mongo Shell….
The mongo shell is an interactive JavaScript
shell for MongoDB
 Run ‘mongo’ command to start mongo shell
 use <your-database-name>
The above is supposed to create a database called <your-
database-name> but … MongoDB creates databases on-
demand. It will get created only when we add something to it
 db.<your-collection-name>.save(<your-json-document>)
The above statement will create a collection called <your-
collection-name>.under the database ‘<your-database-name>
 Insert
 Find
 Find (with filter)
 FindOne
 Update
 Remove
Performance
Indexes support the efficient execution of
queries in MongoDB (Default _id, Single Field,
Compound Index, Multikey Index, Geospatial
Indexes,Text Indexes,Hashed Indexes)
Aggregation
Documents enter a multi-stage pipeline that transforms the
documents into an aggregated results.
http://docs.mongodb.org/manual/core/aggregation-pipeline/
Map-reduce is a data processing paradigm for condensing large
volumes of data into useful aggregated results
http://docs.mongodb.org/manual/core/map-reduce/
Let's go over
 Replication
 Sharding
 Training
https://www.mongodb.org/
https://university.mongodb.com/
 Database-as-a-Service
https://mongolab.com
https://www.compose.io/
MongoDB for Developers
by Ciro Donato Caiazzo
Bologna, July 21, 2015

MongoDB for Developers

  • 1.
    by Ciro DonatoCaiazzo Bologna, July 21, 2015 MongoDB for Developers
  • 2.
    { "Name" : "CiroDonato", "Surname": "Caiazzo", "I am": "Software Engineer", "Working in": "SCAI Consulting", "With Title": "Senior Developer Consultant", "ContactMethods" : [ {"Type": "Twitter", "Value": "@CyrusD87"}, {"Type": "Linkedin", "Value": "Ciro Donato Caiazzo"}, {"Type": "Personal e-mail", "Value": "cd.caiazzo@gmail.com"}, {"Type": "Job e-mail", "Value": "ciro.caiazzo@scai-consulting.grupposcai.it"} ], "Some passions": ["Tecnology","Innovation","Agile","Big data"] }
  • 3.
  • 4.
    NoSQL developed inresponse to a rise in the volume of data… …with a focus on speed, performance, flexibility and scalability… … for the new modern applications.
  • 5.
     NoSQL datamodel addresses issues that relational model cannot achieve (ex. It manage Large volumes of structured, semi- structured, and unstructured data, etc…)  NoSQL databases are more scalable and provide superior performance
  • 6.
  • 7.
  • 8.
     MongoDB isa non relational datastore for JSON documents  Written in C++  License GNU AGPL v3.0 (more..) MongoDBBSON { "skills":{ "database":[ { "name":"Sql", "years":"7" }, { "name":"MongoDB", "years":"2" } ] } }
  • 9.
     MongoDB usescollections for storing groups of data.  A collection can be thought as a table in a relational database. MongoDB Collection 2 Collection 3 Collection 1 RDBMS Table 1 Table 2 Table 3
  • 10.
     Schemaless meansthat two documents that don't have the same schema can be stored in the same collection. {"_id" : "1", "skill" : {"web": [{"name": "html"}, {"name": "css"}]}} {"_id" : "2", "skill" : {"database": [{"name": "mongoDB"}, {"name": "sql server"}]}} {"_id" : "3", "skill" : {"methodologies": ["scrum","kanban","xp"]}}
  • 11.
    MongoDB does notsupport:  Joins;  Transactions across multiple collections But each operation is atomic at level of single document Reason?? Scalability!
  • 12.
    In MongoDB, documentsstored in a collection require a unique _id field that acts as a primary key ObjectId is a 12-byte BSON type, constructed using:  a 4-byte value representing the seconds since the Unix epoch,  a 3-byte machine identifier,  a 2-byte process id, and  a 3-byte counter, starting with a random value.
  • 13.
    What does ithappen for date after the 2038? (Year 2038 problem) On Unix systems, dates are represented as seconds from 1 January 1970 and usually stored in the C time_t type. MongoDB does not use time_t to represent dates internally so do not suffer from this problem http://api.mongodb.org/python/2.1.1/faq.html
  • 14.
  • 15.
    InWindows  Download andinstall MongoDB  https://www.mongodb.org/downloads
  • 16.
     Include MongoDB(bin) path in the environment variable ‘PATH’  Open Command Prompt and run the following command to create the directories where MongoDB will save the data:
  • 17.
    Run ‘mongod’ And now?Go to the Mongo Shell….
  • 18.
    The mongo shellis an interactive JavaScript shell for MongoDB  Run ‘mongo’ command to start mongo shell
  • 19.
     use <your-database-name> Theabove is supposed to create a database called <your- database-name> but … MongoDB creates databases on- demand. It will get created only when we add something to it  db.<your-collection-name>.save(<your-json-document>) The above statement will create a collection called <your- collection-name>.under the database ‘<your-database-name>
  • 20.
  • 21.
     Find (withfilter)  FindOne
  • 22.
  • 23.
  • 24.
    Indexes support theefficient execution of queries in MongoDB (Default _id, Single Field, Compound Index, Multikey Index, Geospatial Indexes,Text Indexes,Hashed Indexes)
  • 25.
  • 26.
    Documents enter amulti-stage pipeline that transforms the documents into an aggregated results. http://docs.mongodb.org/manual/core/aggregation-pipeline/
  • 27.
    Map-reduce is adata processing paradigm for condensing large volumes of data into useful aggregated results http://docs.mongodb.org/manual/core/map-reduce/
  • 28.
  • 29.
  • 30.
  • 31.
    MongoDB for Developers byCiro Donato Caiazzo Bologna, July 21, 2015

Editor's Notes

  • #7 There are other kinds of NoSQL: Geospatial, File System, Object, in-memory
  • #9 JSON (Javascript Object Notation) is an open standard format to transmit data between a server and web application.
  • #10 A collection contains a set of JSON documents (also with sub-documents)
  • #13 Unix epoch starts the 1° of january 1960
  • #14 Unix epoch starts the 1° of january 1960
  • #17 \data \data\db Are directories where mongoDB save datas (just like schema, collections, log, etc)
  • #18 ‘mongod’ is the primary daemon process for the MongoDB system
  • #19 Mongo shell by default is connected to schema ‘test’
  • #20 The save() method uses either the ‘insert or the ’update’ command If the document does not contain an _id field, then the save() method calls the insert() method. During the operation, the mongo shell will create an ObjectId and assign it to the _id field.
  • #22 FindOne() returns only one document that matches the query criteria, if there are multiple documents that satisfy the criterias, the command will return only the first document according to the natural order which reflects the order of documents on the disk.
  • #23 justOne (Optional). To limit the deletion to just one document, set to true. Omit to use the default value of false and delete all documents matching the deletion criteria.