SlideShare une entreprise Scribd logo
1  sur  43
Télécharger pour lire hors ligne
MONGODB
INTRODUCTION
WHAT IS MONGODB ?
MongoDB is :
a document store,
distributed,
and using JSON.
ABOUT MONGODB
2007 : Creation
2010 : First version
2015 : MongoDB 3.0
BASICS
ARCHITECTURE
JSON FORMAT
{
'name':'I’mastring'
'number':12
'array_of_string':['bernard','joe']
}
INSTALLATION
All instructions are on  .MongoDB website
CMD / SERVER
Open a shell and start the server :
$mongod
CMD / IMPORT
IMPORT THE DATASET
https://raw.githubusercontent.com/fabienvauchelles/stweb‐
mongodb/master/data/clients.json
IMPORT IN MONGODB
In a new shell :
$mongoimport--dbmyapp--collectionclients--jsonArray--fileclients.json
CMD / START
Start the command line client :
$mongo
CMD / DATABASES
Show databases :
$showdbs
Use a specific database :
$usemyapp
Show collections :
$showcollections
CMD / FIND
Show one document of a collection :
db.users.findOne()
Show all documents of a collection, without formatting :
db.users.find()
Show all documents of a collection, with formatting :
db.users.find().pretty()
C(R)UD
SYNTAX
db.<collection>.<command>
INSERT A DOCUMENT
Insert 3 documents :
db.users.insert({name:"Grumpy",country:"France",age:2,gender:"M"})
db.users.insert({name:"Lizzy",country:"USA",age:1,gender:"F"})
Get the document :
db.users.findOne({name:"Grumpy"})
The _id field is a unique key (type: ObjectId)
REMOVE A DOCUMENT
db.users.insert({name:"Samy",country:"DK",age:3,gender:"M"})
db.users.remove({name:"Samy"})
REPLACE A DOCUMENT BY ANOTHER
Update all fields of 1 document :
db.users.update({name:"Grumpy"},{name:"Sleepy"})
Get the document :
db.users.findOne({name:"Grumpy"})
db.users.findOne({name:"Sleepy"})
One document, with only field name !
UPDATE ONLY 1 FIELD OF A
DOCUMENT
Update 1 field of 1 document :
db.users.update({name:"Lizzy"},{$set:{country:"Russia"}})
Get the document :
db.users.findOne({name:"Lizzy"})
UPDATE MULTIPLE DOCUMENTS
Insert 3 documents :
db.users.insert({name:"Bernard"})
db.users.insert({name:"Bernard"})
db.users.insert({name:"Bernard"})
Update the first matching document :
db.users.update({name:"Bernard"},{$set:{name:"Sarah"}})
db.users.find({name:"Bernard"})
Update all documents :
db.users.update({name:"Bernard"},{$set:{name:"Sarah"}},{multi:true})
db.users.find({name:"Bernard"})
db.users.find({name:"Sarah"})
ADD AN ITEM AT THE END OF AN
ARRAY
Insert 1 document with an array :
db.users.insert({name:"Marty",hobbies:["hates","loves"]})
db.users.findOne({name:"Marty"})
Add an item at the end of an array :
db.users.update({name:"Marty"},{$push:{hobbies:"fishing"}})
db.users.findOne({name:"Marty"})
REMOVE LAST ITEM OF AN ARRAY
db.users.update({name:"Marty"},{$pop:{hobbies:1}})
db.users.findOne({name:"Marty"})
REMOVE 1 ITEM OF AN ARRAY
db.users.update({name:"Marty"},{$pull:{hobbies:"hates"}})
db.users.findOne({name:"Marty"})
ADD AN ITEM IN THE ARRAY IF IT
DOES NOT ALREADY EXIST
db.users.update({name:"Marty"},{$addToSet:{hobbies:"cycling"}})
db.users.update({name:"Marty"},{$addToSet:{hobbies:"cycling"}})
db.users.findOne({name:"Marty"})
ADVANCED SEARCH
LOGICAL OPERATOR / $AND
$and is implied.
All women, from Russia, 50 yo :
db.clients.find({$and:[
{gender:"F"},
{country:"Russia"},
{age:50}
]})
is equals to :
db.clients.find({
gender:"F",
country:"Russia",
age:50
})
LOGICAL OPERATOR / $OR
All women OR from Russia OR 50 yo :
db.clients.find({$or:[
{gender:"F"},
{country:"Russia"},
{age:50}
]})
COMPARISON OPERATORS / $LT & $LTE
All clients with less or equals than 8 years :
db.clients.find({age:{$lte:8}})
COMPARISON OPERATORS / $GT & $GTE
All clients with more than 76 years (strictly) :
db.clients.find({age:{$gt:76}})
$EXISTS FIELD OPERATOR
All clients with have filled their age :
db.clients.find({age:{$exists:true}})
ARRAY OPERATOR / $ALL (AND)
db.users.insert({name:"John",hobbies:["auto","game"]})
db.users.insert({name:"Nicolas",hobbies:["game"]})
All users who practices "auto" AND "game" :
db.users.find({hobbies:{$all:["auto","game"]}})
ARRAY OPERATOR / $IN (OR)
All users who practices "auto" OR "game" :
db.users.find({hobbies:{$in:["auto","game"]}})
SORT
All clients, sorted by ascending country and descending name :
db.clients.find().sort({country:1,name:-1})
1 = Ascending order
‐1 = Descending order
COUNT
To count the number of occurrences replaces find by count :
db.clients.find({age:{$gt:76}})
becomes :
db.clients.count({age:{$gt:76}})
AGGREGATES
PIPE
Mongos aggregates is a pipe of operations.
EXAMPLE
I want the count of young people (20‐30) for each city :
SYNTAX
db.<collection>.aggregate([{$op-1},{$op-2},..,{$op-n}])
FILTERS / $MATCH
Same filter that is used in find, update, etc.
db.clients.aggregate([
{$match:{age:{$gt:70}}}
])
PROJECTIONS / $PROJECT
$project chooses returned fields.
db.clients.aggregate([
{$project:{name:1}}
])
Remark: by default, _id is always in the projection and other
fields are not.
DUPLICATES / $UNWIND
$unwind repeats a document for each item of an array.
db.users.insert({name:"Sam",hobbies:["cycle","boat","computer"]})
db.users.aggregate([
{$match:{name:"Sam"}},
{$unwind:"$hobbies"}
])
GROUPS ($GROUP)
$group is SQL group by.
$group performs operations on similar documents
Operations: $sum, $avg, $first, $last, $min, $max, $push,
$addToSet
db.clients.aggregate([
{$group:{_id:"$country",maxage:{$max:"$age"}}}
])
db.clients.aggregate([
{$group:{_id:"$country",maxage:{$max:"$age"}}},
{$sort:{_id:1}},
{$project:{_id:0,country:"$_id",maxage:1}}
])
OTHERS OPERATIONS
There is other operations : $sort, $limit, $skip, etc.
 is your friend !MongoDB documentation
© Fabien Vauchelles (2015)
GO FURTHER
MongoDB University

Contenu connexe

Tendances

Working with web_services
Working with web_servicesWorking with web_services
Working with web_services
Lorna Mitchell
 
Sample file processing
Sample file processingSample file processing
Sample file processing
Issay Meii
 
MongoUK - PHP Development
MongoUK - PHP DevelopmentMongoUK - PHP Development
MongoUK - PHP Development
Boxed Ice
 
第一回MongoDBソースコードリーディング
第一回MongoDBソースコードリーディング第一回MongoDBソースコードリーディング
第一回MongoDBソースコードリーディング
nobu_k
 
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard InterfacesDecoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Thomas Weinert
 

Tendances (19)

A new way to develop with WordPress!
A new way to develop with WordPress!A new way to develop with WordPress!
A new way to develop with WordPress!
 
Working with web_services
Working with web_servicesWorking with web_services
Working with web_services
 
Implementing New Web
Implementing New WebImplementing New Web
Implementing New Web
 
Introducere in web
Introducere in webIntroducere in web
Introducere in web
 
Sample file processing
Sample file processingSample file processing
Sample file processing
 
20170310 PHP goal pyramid for memorising
20170310 PHP goal pyramid for memorising20170310 PHP goal pyramid for memorising
20170310 PHP goal pyramid for memorising
 
File handling complete programs in c++
File handling complete programs in c++File handling complete programs in c++
File handling complete programs in c++
 
RegistryModClass
RegistryModClassRegistryModClass
RegistryModClass
 
Flask intro - ROSEdu web workshops
Flask intro - ROSEdu web workshopsFlask intro - ROSEdu web workshops
Flask intro - ROSEdu web workshops
 
Boot strap.groovy
Boot strap.groovyBoot strap.groovy
Boot strap.groovy
 
MongoUK - PHP Development
MongoUK - PHP DevelopmentMongoUK - PHP Development
MongoUK - PHP Development
 
Node lt
Node ltNode lt
Node lt
 
第一回MongoDBソースコードリーディング
第一回MongoDBソースコードリーディング第一回MongoDBソースコードリーディング
第一回MongoDBソースコードリーディング
 
The new features of PHP 7
The new features of PHP 7The new features of PHP 7
The new features of PHP 7
 
Joy of Unix
Joy of UnixJoy of Unix
Joy of Unix
 
MySQLi - An Improved Extension of MySQL
MySQLi - An Improved Extension of MySQLMySQLi - An Improved Extension of MySQL
MySQLi - An Improved Extension of MySQL
 
わかった気になるgitit-0.8
わかった気になるgitit-0.8わかった気になるgitit-0.8
わかった気になるgitit-0.8
 
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard InterfacesDecoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
 
Understanding the Node.js Platform
Understanding the Node.js PlatformUnderstanding the Node.js Platform
Understanding the Node.js Platform
 

Similaire à Using MongoDB

Building Hybrid data cluster using PostgreSQL and MongoDB
Building Hybrid data cluster using PostgreSQL and MongoDBBuilding Hybrid data cluster using PostgreSQL and MongoDB
Building Hybrid data cluster using PostgreSQL and MongoDB
Ashnikbiz
 
introtomongodb
introtomongodbintrotomongodb
introtomongodb
saikiran
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
Alex Bilbie
 

Similaire à Using MongoDB (20)

Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
 
Lab2-DB-Mongodb
Lab2-DB-MongodbLab2-DB-Mongodb
Lab2-DB-Mongodb
 
MongoDB and Node.js
MongoDB and Node.jsMongoDB and Node.js
MongoDB and Node.js
 
Building Hybrid data cluster using PostgreSQL and MongoDB
Building Hybrid data cluster using PostgreSQL and MongoDBBuilding Hybrid data cluster using PostgreSQL and MongoDB
Building Hybrid data cluster using PostgreSQL and MongoDB
 
MongoDB basics & Introduction
MongoDB basics & IntroductionMongoDB basics & Introduction
MongoDB basics & Introduction
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Introduction to MongoDB with PHP
Introduction to MongoDB with PHPIntroduction to MongoDB with PHP
Introduction to MongoDB with PHP
 
오픈 소스 프로그래밍 - NoSQL with Python
오픈 소스 프로그래밍 - NoSQL with Python오픈 소스 프로그래밍 - NoSQL with Python
오픈 소스 프로그래밍 - NoSQL with Python
 
The End of Dinosaurs happened because of [a] Meteor
The End of Dinosaurs happened because of [a] MeteorThe End of Dinosaurs happened because of [a] Meteor
The End of Dinosaurs happened because of [a] Meteor
 
MongoDB
MongoDBMongoDB
MongoDB
 
MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...
MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...
MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...
 
MongoDB and DynamoDB
MongoDB and DynamoDBMongoDB and DynamoDB
MongoDB and DynamoDB
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 
Mongo-Drupal
Mongo-DrupalMongo-Drupal
Mongo-Drupal
 
Getting started with node JS
Getting started with node JSGetting started with node JS
Getting started with node JS
 
Spring Data MongoDB 介紹
Spring Data MongoDB 介紹Spring Data MongoDB 介紹
Spring Data MongoDB 介紹
 
introtomongodb
introtomongodbintrotomongodb
introtomongodb
 
Getting started with MongoDB and Scala - Open Source Bridge 2012
Getting started with MongoDB and Scala - Open Source Bridge 2012Getting started with MongoDB and Scala - Open Source Bridge 2012
Getting started with MongoDB and Scala - Open Source Bridge 2012
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 

Plus de Fabien Vauchelles (9)

Use Node.js to create a REST API
Use Node.js to create a REST APIUse Node.js to create a REST API
Use Node.js to create a REST API
 
Design a landing page
Design a landing pageDesign a landing page
Design a landing page
 
De l'idée au produit en lean startup IT
De l'idée au produit en lean startup ITDe l'idée au produit en lean startup IT
De l'idée au produit en lean startup IT
 
Créer une startup
Créer une startupCréer une startup
Créer une startup
 
What is NoSQL ?
What is NoSQL ?What is NoSQL ?
What is NoSQL ?
 
Create a landing page
Create a landing pageCreate a landing page
Create a landing page
 
Master AngularJS
Master AngularJSMaster AngularJS
Master AngularJS
 
Discover AngularJS
Discover AngularJSDiscover AngularJS
Discover AngularJS
 
Comment OAuth autorise ces applications à accéder à nos données privées ?
Comment OAuth autorise ces applications à accéder à nos données privées ?Comment OAuth autorise ces applications à accéder à nos données privées ?
Comment OAuth autorise ces applications à accéder à nos données privées ?
 

Dernier

Dernier (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Using MongoDB