SlideShare a Scribd company logo
Conception d'Applications
Interactives :
Applications Web et JEE
Séance #4
Une forge JavaScript
2/4 - MongoDb and NoSQL
Introduction à MongoDB
What's MongoDB
open-source document database
that provides high performance,
high availability,
and automatic scaling
Document database
A record in MongoDB is a document, which is a
data structure composed of field and value
pairs
Documents are similar to JSON objects
Document database
Documents are stored in collections
Collections share common indexes
Collections & documents
● No predefined schema
○ Documents in a collection can have different fields
○ Fields can be added, modified or deleted at any time
● Documents follow BSON (JSON-like) format
○ Key-value pairs (hashes)
{
"_id": ObjectId("223EBC5477A124425"),
"Last Name": "Gonzalez",
Insert data
Query data
● db.collection.find()
It returns a cursor, an iterable object
Query data
Projections
RDBMS vs MongoDB
RDBMS
● Databases have tables
● Tables have rows
● Rows have cell
● Cells contain types simples
● Schemas are rigid
MongoDB
● Database have collections
● Collections have documents
● Documents have fields
● Fields contain
○ Types simples
○ Arrays
○ Other documents
● Schemas are fluid
Key features
● High performance
● High availability
● Automatic scaling
High availability
● Election at initialization or when primary lost
Installing MongoDB
1. Download MongoDB http://www.mongodb.org/downloads
2. Install the msi (win) or uncompress the tgz
(linux)
e.g. C:mongodb or /opt/mongodb
3. Create a data directory
e.g. /opt/mongodb_data or C:mongodb_data
4. Run mongo demon
Running MongoDD
horacio@horacio-xps:~$ mongod --dbpath /opt/data/
2015-05-06T23:46:14.392+0200 I JOURNAL [initandlisten] journal dir=/opt/data/journal
2015-05-06T23:46:14.394+0200 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2015-05-06T23:46:14.431+0200 I CONTROL [initandlisten] MongoDB starting : pid=5493 port=27017 dbpath=/opt/data/ 64-bit
host=horacio-xps
2015-05-06T23:46:14.431+0200 I CONTROL [initandlisten] db version v3.0.2
2015-05-06T23:46:14.431+0200 I CONTROL [initandlisten] git version: 6201872043ecbbc0a4cc169b5482dcf385fc464f
2015-05-06T23:46:14.431+0200 I CONTROL [initandlisten] build info: Linux build6.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP
Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-05-06T23:46:14.431+0200 I CONTROL [initandlisten] allocator: tcmalloc
2015-05-06T23:46:14.431+0200 I CONTROL [initandlisten] options: { storage: { dbPath: "/opt/data/" } }
2015-05-06T23:46:14.433+0200 I STORAGE [initandlisten] info openExisting file size 16777216 but mmapv1GlobalOptions.
smallfiles=false: /opt/data/startupweekendbrest.0
2015-05-06T23:46:14.440+0200 I INDEX [initandlisten] allocating new ns file /opt/data/local.ns, filling with zeroes...
2015-05-06T23:46:14.532+0200 I STORAGE [FileAllocator] allocating new datafile /opt/data/local.0, filling with zeroes...
2015-05-06T23:46:14.532+0200 I STORAGE [FileAllocator] creating directory /opt/data/_tmp
2015-05-06T23:46:14.572+0200 I STORAGE [FileAllocator] done allocating datafile /opt/data/local.0, size: 64MB, took 0.023 secs
2015-05-06T23:46:14.585+0200 I NETWORK [initandlisten] waiting for connections on port 27017
Tools in MongoDB
● mongod - primary daemon process
● mongo - command-line client
● mongostat - command-line stats summary
● mongotop - command-line performance
tracker
Connecting to a MongoDB instance
mongo --host 127.0.0.1 --port 27017
or using default parameters
mongo
● Default host: 127.0.0.1
● Default port: 27017
horacio@horacio-xps:~$ mongo
MongoDB shell version: 3.0.2
First steps
● To view available databases:
> show dbs
local 0.078GB
test 0.031GB
● To choose a database:
> use test
switched to db test
First steps
● To check what's the current database:
> db
● To get some help:
> help
● To show the collections within a database:
> show collections
Enter some data
> a = {"Last Name": "Gonzalez","First Name": "Horacio","Date of Birth": "1976-05-
05" }
{
"Last Name" : "Gonzalez",
"First Name" : "Horacio",
"Date of Birth" : "1976-05-05"
}
> db.test.insert(a)
WriteResult({ "nInserted" : 1 })
> b={"Field A": "Value A", "Field B": "Value B"}
{ "Field A" : "Value A", "Field B" : "Value B" }
> db.test.insert(b)
WriteResult({ "nInserted" : 1 })
Query data
● Find all the elements in a collection
> db.test.find()
{ "_id" : ObjectId("554a9944b5091037c44dddcc"), "Last Name" : "Gonzalez",
"First Name" : "Horacio", "Date of Birth" : "1976-05-05" }
{ "_id" : ObjectId("554a998eb5091037c44dddcd"), "Field A" : "Value A", "Field
B" : "Value B" }
_id
● Primary key
● Automatically indexed
● Generated as an ObjectId if not provided
● Must be unique and immutable
ObjectId: Special 12 byte value unique across cluster
Using JavaScript in mongo
> for(var i=0; i<5; i++) db.test.insert({a:42, b:i})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("554a990f0ebf783b63a57776"), "Last Name" : "Gonzalez", "First
Name" : "Horacio", "Date of Birth" : "1976-05-05" }
{ "_id" : ObjectId("554a9944b5091037c44dddcc"), "Last Name" : "Gonzalez", "First
Name" : "Horacio", "Date of Birth" : "1976-05-05" }
{ "_id" : ObjectId("554a998eb5091037c44dddcd"), "Field A" : "Value A", "Field B" :
"Value B" }
{ "_id" : ObjectId("554a9c21b5091037c44dddce"), "a" : 42, "b" : 0 }
{ "_id" : ObjectId("554a9c21b5091037c44dddcf"), "a" : 42, "b" : 1 }
{ "_id" : ObjectId("554a9c21b5091037c44dddd0"), "a" : 42, "b" : 2 }
{ "_id" : ObjectId("554a9c21b5091037c44dddd1"), "a" : 42, "b" : 3 }
{ "_id" : ObjectId("554a9c21b5091037c44dddd2"), "a" : 42, "b" : 4 }
Query for specific documents
> db.test.find({"Field A": "Value A"})
{ "_id" : ObjectId("554a998eb5091037c44dddcd"), "Field A" : "Value A", "Field B" :
"Value B" }
> db.test.find({ b: { $gt: 2 } }).sort({ b: -1 })
{ "_id" : ObjectId("554a9c21b5091037c44dddd2"), "a" : 42, "b" : 4 }
{ "_id" : ObjectId("554a9c21b5091037c44dddd1"), "a" : 42, "b" : 3 }
Conditional operators:
$all, $exists, $type, $mod,
$or, $and, $not, $nor $size,
$eq, $ne, $lt, $lte, $gt, $gte, $in, $nin...
Querying with RegEx
> db.test.findOne({ "Last Name": /Gon/})
{
"_id" : ObjectId("554a990f0ebf783b63a57776"),
"Last Name" : "Gonzalez",
"First Name" : "Horacio",
"Date of Birth" : "1976-05-05"
}
Operations
> db.test.insert(record)
> db.test.find(query)[.skip(X)][.limit
(Y)]
> db.test.findOne(query)
> db.test.remove(query[,
justone=false])
Creating index
> db.test.ensureIndex({ b: 1 })
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
MongoDB is fully featured
Exercice 1
● Install MongoDB
● Run mongod
● Connect using mongo
● Create a collection, add some object, tests
some queries
References
Exercice 2
Model and create a collection to store the beer
data
for the Angular-Beers project
https://github.com/LostInBrittany/angular-beers
● Tests some queries

More Related Content

PDF
MySQL Without The SQL -- Oh My! PHP Detroit July 2018
PPTX
Mongo db – document oriented database
PPTX
MongoDb and NoSQL
ODP
MongoDB - A Document NoSQL Database
PDF
Indexing
KEY
PPTX
Back to Basics Webinar 2: Your First MongoDB Application
PPTX
Back to Basics Webinar 3: Schema Design Thinking in Documents
MySQL Without The SQL -- Oh My! PHP Detroit July 2018
Mongo db – document oriented database
MongoDb and NoSQL
MongoDB - A Document NoSQL Database
Indexing
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 3: Schema Design Thinking in Documents

What's hot (19)

KEY
Optimize drupal using mongo db
PPTX
Discover the Power of the NoSQL + SQL with MySQL
ODP
MongoDB : The Definitive Guide
PDF
Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting
PDF
NoSQL - An introduction to CouchDB
PPT
Introduction to couch_db
PPTX
PDF
Mongo Presentation by Metatagg Solutions
PPTX
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
PDF
CouchDB Open Source Bridge
PDF
An introduction to MongoDB
PPT
PhpstudyTokyo MongoDB PHP CakePHP
KEY
MongoDB at GUL
PPTX
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
PPT
Introduction to MongoDB
KEY
Schema Design with MongoDB
PDF
MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...
PPTX
Webinar: Schema Design
PPTX
MongoDB + Java + Spring Data
Optimize drupal using mongo db
Discover the Power of the NoSQL + SQL with MySQL
MongoDB : The Definitive Guide
Apache CouchDB Presentation @ Sept. 2104 GTALUG Meeting
NoSQL - An introduction to CouchDB
Introduction to couch_db
Mongo Presentation by Metatagg Solutions
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
CouchDB Open Source Bridge
An introduction to MongoDB
PhpstudyTokyo MongoDB PHP CakePHP
MongoDB at GUL
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Introduction to MongoDB
Schema Design with MongoDB
MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...
Webinar: Schema Design
MongoDB + Java + Spring Data
Ad

Viewers also liked (8)

PDF
GDG Rennes - Bootcamp Initiation Android - Théorie
PDF
GDG Rennes - Bootcamp Initiation Android - Hello World with Events and Intents
PDF
Bootcamp d'Initiation à Android - 2013/11/30 - Live coding : Hello world!
PDF
GDG Rennes - Bootcamp Initiation Android - Hello World
PDF
Bootcamp d'Initiation à Android - 2013/11/30
PDF
FinistJUG - J’ai besoin d’une appli web rapidement
PDF
BreizhBeans - Web components
PPT
áLbum seriado ds ts
GDG Rennes - Bootcamp Initiation Android - Théorie
GDG Rennes - Bootcamp Initiation Android - Hello World with Events and Intents
Bootcamp d'Initiation à Android - 2013/11/30 - Live coding : Hello world!
GDG Rennes - Bootcamp Initiation Android - Hello World
Bootcamp d'Initiation à Android - 2013/11/30
FinistJUG - J’ai besoin d’une appli web rapidement
BreizhBeans - Web components
áLbum seriado ds ts
Ad

Similar to ENIB 2015-2016 - CAI Web - S01E01- MongoDB and NoSQL (20)

PDF
ENIB 2015 2016 - CAI Web S02E03 - Forge JS 2/4 - MongoDB and NoSQL
PDF
Starting with MongoDB
PPTX
Webinar: General Technical Overview of MongoDB for Dev Teams
PPTX
Introduction to MongoDB
PPTX
171_74_216_Module_5-Non_relational_database_-mongodb.pptx
PPTX
MongoDb Database Notes Presentation Latest
PDF
PPTX
Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
PPTX
Back to Basics Webinar 2 - Your First MongoDB Application
PDF
MongoDB for Coder Training (Coding Serbia 2013)
PPTX
Back to Basics, webinar 2: La tua prima applicazione MongoDB
PDF
The emerging world of mongo db csp
PDF
Back to Basics 2017: Mí primera aplicación MongoDB
KEY
Mongodb intro
PPTX
Introduction to MongoDB – A NoSQL Database
PDF
Intro to MongoDB and datamodeling
PPT
Mongo db basics
PDF
10gen Presents Schema Design and Data Modeling
PDF
Building your first app with MongoDB
PPT
mongodb crud operations with detailed.ppt
ENIB 2015 2016 - CAI Web S02E03 - Forge JS 2/4 - MongoDB and NoSQL
Starting with MongoDB
Webinar: General Technical Overview of MongoDB for Dev Teams
Introduction to MongoDB
171_74_216_Module_5-Non_relational_database_-mongodb.pptx
MongoDb Database Notes Presentation Latest
Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
Back to Basics Webinar 2 - Your First MongoDB Application
MongoDB for Coder Training (Coding Serbia 2013)
Back to Basics, webinar 2: La tua prima applicazione MongoDB
The emerging world of mongo db csp
Back to Basics 2017: Mí primera aplicación MongoDB
Mongodb intro
Introduction to MongoDB – A NoSQL Database
Intro to MongoDB and datamodeling
Mongo db basics
10gen Presents Schema Design and Data Modeling
Building your first app with MongoDB
mongodb crud operations with detailed.ppt

More from Horacio Gonzalez (20)

PDF
Il n'y a pas que Polymer dans la vie… - RennesJS - 2017-06-27
PDF
But there is no web component for that - Web Components Remote Conference - 2...
PDF
Mixité dans le monde des WebComponents - DevFest Toulouse - 2017-09-27
PDF
ENIB 2016 2017 - CAI Web S02E01- Côté navigateur 2/3 - HTML5, CSS3, Twitter B...
PDF
ENIB 2016 2017 - CAI Web S02E01- Côté navigateur 1/3 - HTTP, HTML, CSS, JS
PDF
Battle of Frameworks: Polymer - Meetup Paris Web Components - 2016-09
PDF
Mixing Web Components - Best of Web Paris - 2016 06-09
PDF
Polymer in the real life - Devoxx France - 2016 04-20
PDF
Warp10: collect, store and manipulate sensor data - BreizhCamp - 2016 03-24
PDF
ENIB 2015 2016 - CAI Web S02E03- Forge JS 1/4 - La forge JavaScript
PDF
ENIB 2015 2016 - CAI Web S02E01- Côté Navigateur 3/3 - Web Components avec Po...
PDF
ENIB 2015 2016 - CAI Web S02E01- Côté Navigateur 2/3 - HTML5 / CSS3 / Twitter...
PDF
ENIB 2015 2016 - CAI Web S02E01- Côté Navigateur 1/3 - HTTP, HTML, CSS JS
PDF
ENIB 2015-2016 - CAI Web - S01E01- La forge JavaScript
PDF
ENIB 2015-2016 - CAI Web - S01E01- Côté navigateur 2/3 - HTML5, CSS3, Twitte...
PDF
ENIB 2015-2016 - CAI Web - S01E01- Côté navigateur 1/3 - HTTP, HTML, CSS, JS
PDF
ENIB 2015-2016 - CAI Web - S01E01- Côté navigateur 3/3 - Web components avec ...
PDF
Beyond Polymer - JUG Summer Camp - 2015-09-18
PDF
Mixing Web Components - Paris Web Components - 2015 09-16
PDF
Devoxx France - Web Components, Polymer et Material Design
Il n'y a pas que Polymer dans la vie… - RennesJS - 2017-06-27
But there is no web component for that - Web Components Remote Conference - 2...
Mixité dans le monde des WebComponents - DevFest Toulouse - 2017-09-27
ENIB 2016 2017 - CAI Web S02E01- Côté navigateur 2/3 - HTML5, CSS3, Twitter B...
ENIB 2016 2017 - CAI Web S02E01- Côté navigateur 1/3 - HTTP, HTML, CSS, JS
Battle of Frameworks: Polymer - Meetup Paris Web Components - 2016-09
Mixing Web Components - Best of Web Paris - 2016 06-09
Polymer in the real life - Devoxx France - 2016 04-20
Warp10: collect, store and manipulate sensor data - BreizhCamp - 2016 03-24
ENIB 2015 2016 - CAI Web S02E03- Forge JS 1/4 - La forge JavaScript
ENIB 2015 2016 - CAI Web S02E01- Côté Navigateur 3/3 - Web Components avec Po...
ENIB 2015 2016 - CAI Web S02E01- Côté Navigateur 2/3 - HTML5 / CSS3 / Twitter...
ENIB 2015 2016 - CAI Web S02E01- Côté Navigateur 1/3 - HTTP, HTML, CSS JS
ENIB 2015-2016 - CAI Web - S01E01- La forge JavaScript
ENIB 2015-2016 - CAI Web - S01E01- Côté navigateur 2/3 - HTML5, CSS3, Twitte...
ENIB 2015-2016 - CAI Web - S01E01- Côté navigateur 1/3 - HTTP, HTML, CSS, JS
ENIB 2015-2016 - CAI Web - S01E01- Côté navigateur 3/3 - Web components avec ...
Beyond Polymer - JUG Summer Camp - 2015-09-18
Mixing Web Components - Paris Web Components - 2015 09-16
Devoxx France - Web Components, Polymer et Material Design

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PPTX
Pharma ospi slides which help in ospi learning
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Structure & Organelles in detailed.
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPH.pptx obstetrics and gynecology in nursing
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
human mycosis Human fungal infections are called human mycosis..pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
2.FourierTransform-ShortQuestionswithAnswers.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Cell Types and Its function , kingdom of life
Pharma ospi slides which help in ospi learning
Microbial disease of the cardiovascular and lymphatic systems
Cell Structure & Organelles in detailed.
O5-L3 Freight Transport Ops (International) V1.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial diseases, their pathogenesis and prophylaxis
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

ENIB 2015-2016 - CAI Web - S01E01- MongoDB and NoSQL

  • 1. Conception d'Applications Interactives : Applications Web et JEE Séance #4 Une forge JavaScript 2/4 - MongoDb and NoSQL
  • 3. What's MongoDB open-source document database that provides high performance, high availability, and automatic scaling
  • 4. Document database A record in MongoDB is a document, which is a data structure composed of field and value pairs Documents are similar to JSON objects
  • 5. Document database Documents are stored in collections Collections share common indexes
  • 6. Collections & documents ● No predefined schema ○ Documents in a collection can have different fields ○ Fields can be added, modified or deleted at any time ● Documents follow BSON (JSON-like) format ○ Key-value pairs (hashes) { "_id": ObjectId("223EBC5477A124425"), "Last Name": "Gonzalez",
  • 8. Query data ● db.collection.find() It returns a cursor, an iterable object
  • 11. RDBMS vs MongoDB RDBMS ● Databases have tables ● Tables have rows ● Rows have cell ● Cells contain types simples ● Schemas are rigid MongoDB ● Database have collections ● Collections have documents ● Documents have fields ● Fields contain ○ Types simples ○ Arrays ○ Other documents ● Schemas are fluid
  • 12. Key features ● High performance ● High availability ● Automatic scaling
  • 13. High availability ● Election at initialization or when primary lost
  • 14. Installing MongoDB 1. Download MongoDB http://www.mongodb.org/downloads 2. Install the msi (win) or uncompress the tgz (linux) e.g. C:mongodb or /opt/mongodb 3. Create a data directory e.g. /opt/mongodb_data or C:mongodb_data 4. Run mongo demon
  • 15. Running MongoDD horacio@horacio-xps:~$ mongod --dbpath /opt/data/ 2015-05-06T23:46:14.392+0200 I JOURNAL [initandlisten] journal dir=/opt/data/journal 2015-05-06T23:46:14.394+0200 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed 2015-05-06T23:46:14.431+0200 I CONTROL [initandlisten] MongoDB starting : pid=5493 port=27017 dbpath=/opt/data/ 64-bit host=horacio-xps 2015-05-06T23:46:14.431+0200 I CONTROL [initandlisten] db version v3.0.2 2015-05-06T23:46:14.431+0200 I CONTROL [initandlisten] git version: 6201872043ecbbc0a4cc169b5482dcf385fc464f 2015-05-06T23:46:14.431+0200 I CONTROL [initandlisten] build info: Linux build6.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49 2015-05-06T23:46:14.431+0200 I CONTROL [initandlisten] allocator: tcmalloc 2015-05-06T23:46:14.431+0200 I CONTROL [initandlisten] options: { storage: { dbPath: "/opt/data/" } } 2015-05-06T23:46:14.433+0200 I STORAGE [initandlisten] info openExisting file size 16777216 but mmapv1GlobalOptions. smallfiles=false: /opt/data/startupweekendbrest.0 2015-05-06T23:46:14.440+0200 I INDEX [initandlisten] allocating new ns file /opt/data/local.ns, filling with zeroes... 2015-05-06T23:46:14.532+0200 I STORAGE [FileAllocator] allocating new datafile /opt/data/local.0, filling with zeroes... 2015-05-06T23:46:14.532+0200 I STORAGE [FileAllocator] creating directory /opt/data/_tmp 2015-05-06T23:46:14.572+0200 I STORAGE [FileAllocator] done allocating datafile /opt/data/local.0, size: 64MB, took 0.023 secs 2015-05-06T23:46:14.585+0200 I NETWORK [initandlisten] waiting for connections on port 27017
  • 16. Tools in MongoDB ● mongod - primary daemon process ● mongo - command-line client ● mongostat - command-line stats summary ● mongotop - command-line performance tracker
  • 17. Connecting to a MongoDB instance mongo --host 127.0.0.1 --port 27017 or using default parameters mongo ● Default host: 127.0.0.1 ● Default port: 27017 horacio@horacio-xps:~$ mongo MongoDB shell version: 3.0.2
  • 18. First steps ● To view available databases: > show dbs local 0.078GB test 0.031GB ● To choose a database: > use test switched to db test
  • 19. First steps ● To check what's the current database: > db ● To get some help: > help ● To show the collections within a database: > show collections
  • 20. Enter some data > a = {"Last Name": "Gonzalez","First Name": "Horacio","Date of Birth": "1976-05- 05" } { "Last Name" : "Gonzalez", "First Name" : "Horacio", "Date of Birth" : "1976-05-05" } > db.test.insert(a) WriteResult({ "nInserted" : 1 }) > b={"Field A": "Value A", "Field B": "Value B"} { "Field A" : "Value A", "Field B" : "Value B" } > db.test.insert(b) WriteResult({ "nInserted" : 1 })
  • 21. Query data ● Find all the elements in a collection > db.test.find() { "_id" : ObjectId("554a9944b5091037c44dddcc"), "Last Name" : "Gonzalez", "First Name" : "Horacio", "Date of Birth" : "1976-05-05" } { "_id" : ObjectId("554a998eb5091037c44dddcd"), "Field A" : "Value A", "Field B" : "Value B" }
  • 22. _id ● Primary key ● Automatically indexed ● Generated as an ObjectId if not provided ● Must be unique and immutable ObjectId: Special 12 byte value unique across cluster
  • 23. Using JavaScript in mongo > for(var i=0; i<5; i++) db.test.insert({a:42, b:i}) WriteResult({ "nInserted" : 1 }) > db.test.find() { "_id" : ObjectId("554a990f0ebf783b63a57776"), "Last Name" : "Gonzalez", "First Name" : "Horacio", "Date of Birth" : "1976-05-05" } { "_id" : ObjectId("554a9944b5091037c44dddcc"), "Last Name" : "Gonzalez", "First Name" : "Horacio", "Date of Birth" : "1976-05-05" } { "_id" : ObjectId("554a998eb5091037c44dddcd"), "Field A" : "Value A", "Field B" : "Value B" } { "_id" : ObjectId("554a9c21b5091037c44dddce"), "a" : 42, "b" : 0 } { "_id" : ObjectId("554a9c21b5091037c44dddcf"), "a" : 42, "b" : 1 } { "_id" : ObjectId("554a9c21b5091037c44dddd0"), "a" : 42, "b" : 2 } { "_id" : ObjectId("554a9c21b5091037c44dddd1"), "a" : 42, "b" : 3 } { "_id" : ObjectId("554a9c21b5091037c44dddd2"), "a" : 42, "b" : 4 }
  • 24. Query for specific documents > db.test.find({"Field A": "Value A"}) { "_id" : ObjectId("554a998eb5091037c44dddcd"), "Field A" : "Value A", "Field B" : "Value B" } > db.test.find({ b: { $gt: 2 } }).sort({ b: -1 }) { "_id" : ObjectId("554a9c21b5091037c44dddd2"), "a" : 42, "b" : 4 } { "_id" : ObjectId("554a9c21b5091037c44dddd1"), "a" : 42, "b" : 3 } Conditional operators: $all, $exists, $type, $mod, $or, $and, $not, $nor $size, $eq, $ne, $lt, $lte, $gt, $gte, $in, $nin...
  • 25. Querying with RegEx > db.test.findOne({ "Last Name": /Gon/}) { "_id" : ObjectId("554a990f0ebf783b63a57776"), "Last Name" : "Gonzalez", "First Name" : "Horacio", "Date of Birth" : "1976-05-05" }
  • 26. Operations > db.test.insert(record) > db.test.find(query)[.skip(X)][.limit (Y)] > db.test.findOne(query) > db.test.remove(query[, justone=false])
  • 27. Creating index > db.test.ensureIndex({ b: 1 }) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
  • 28. MongoDB is fully featured
  • 29. Exercice 1 ● Install MongoDB ● Run mongod ● Connect using mongo ● Create a collection, add some object, tests some queries References
  • 30. Exercice 2 Model and create a collection to store the beer data for the Angular-Beers project https://github.com/LostInBrittany/angular-beers ● Tests some queries