Transcript
Page 1: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

MongoDB / MongoId

Présentation

Vincent Tabary Aymeric Brisse

Page 2: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

1. NoSQL

2. MongoDB

3. MongoId

4. Bibliographie

Page 3: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

SGBD relationnels limités :

* pour load balancing

* pour une logique de « Big Data »

SGBD démocratisé depuis 2009

Systèmes destinés à Internet

Pourquoi NoSQL ?

Ne supporte pas langage SQL

1. NoSQL

Page 4: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Basé sur le principe des tableaux associatifs

Intégre des technologies :

● clustering

● réplication

Retire certaines logiques :

● langage SQL

● stockage sous forme de matrice

Meilleures performances sur requêtes simples

1. NoSQL

Page 5: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Exemples de serveurs :

● Cassandra : Facebook, Twitter, Digg

● BigTable : Google

● CouchDB

● Redis

● Riak

● SimpleDB : Amazon

● MongoDB

1. NoSQL

Page 6: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Serveur NoSQL open source

Réplication

Sharding : répartition sur des clusters

Requêtes

Implémentation de Map/Reduce (JS)

Peut stocker des fichiers

2. MongoDB

Page 7: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Drivers disponibles pour plusieurs langages :

● Python

● Ruby

● Java

● Javascript (Node.js)

● C++

● PHP

● ...

2. MongoDB

Page 8: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

MongoDB ("humongous" / “énorme”) est une

BDD open source NoSQL écrite en C++

distribuée sous license AGPL.

2. MongoDB - Présentation

Page 9: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Caractéristiques :

- BDD orienté Document : objects structurés de type

JSON sans schéma

- Indexes sur n’importe quel attribut

- Replication & High Availability, Auto-Sharding

(horizontal scale)

- Requêtes orientés document

- Updates rapides : Atomic modifiers

- Map/Reduce : Process des données et agrégations

2. MongoDB - Présentation

Page 10: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

- Objets structurés au format BSON (JSON binaire), sans

schéma prédéterminé.

- Documents enregistrés dans des collections

Collection “Users” { "_id": ObjectId("4efa8d2b7d284dad101e4bc7"), "name": "James Bond", "login": "james", "age": 50, "adress": { "street" : "42 Class Street", "city": "Londres" } }

- Types : string, integer, boolean, double, null, array, object,

date, object id, binary data, regular expression.

2. MongoDB - BDD orientée Document

Page 11: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

- Linked

# collection ‘users’ { "_id": ObjectId("4efa8d2b7d284dad101e4bc7"), "group_ids" : [

ObjectId("50a4f7464efbbe201a000003"),ObjectId("50a4fd234efbbedd1a00000f")

],

}

2. MongoDB - BDD orientée Document

Page 12: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

- Embed (données “pré-jointes”)

# collection ‘users’ { "_id": ObjectId("4efa8d2b7d284dad101e4bc7"), "thumbnails": [ { "_id": ObjectId("4efa8d2b7d284dbc987a789c"), "url": "http://cloud.pm.com/resources/hash.jpg" }, { "_id": ObjectId("4efa8d2b7d284dbc987a789d"), "url": "http://cloud.pm.com/resources/hash2.jpg" }, ]

}

2. MongoDB - BDD orientée Document

Page 13: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

- Récupérer les personnes ayant pour login “james”

> db.users.find( { login : "james" } )

- Récupérer les personnes vivant à Londres

> db.users.find( { adress.city : "Londres" } )

- Récupérer les groupes d’une personne

> var user = db.users.findOne( { _id:

ObjectId("50a4f7264efbbe201a000001") });

> db.groups.find( { _id : { $in : user.groups } });

2. MongoDB - Queries

Page 14: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

- Pas de support de transactions (sharding, dead locks, etc.)

- Mais Atomic Modifiers

$set - set a particular value

$unset - delete a particular field

$inc - increment a particular value by a certain amount

$push - append a value to an array

$pushAll - append several values to an array

$pull - remove a value(s) from an existing array

$pullAll - remove several value(s) from an existing array

$bit - bitwise operations

2. MongoDB - Atomic Modifiers

Page 15: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

- Augmenter le nombre de votes d’un media

> db.medias.update( { _id :

ObjectId("4efa8d2b7d284dad101e4bc7") } , { $inc :

{ votes_count : 1 } }

2. MongoDB - Atomic Modifiers

Page 16: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

- Base de données relationnelles : schéma orienté

“normalisation” (3NF) - Théorique

- MongoDB : schéma orienté “usage” et “performance” -

Pratique

- Les questions à se poser :

● Embed VS Linked ?

● Combien de collections ?

● Quelles opérations atomiques sont nécessaires ?

● Quels indexes créer ?

● Comment partitionner ?

2. MongoDB - Designer sa BDD (Schéma)

Page 17: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

A savoir sur MongoDB :

● Pas d'authentification par défaut

● Version 32 bits limitée à 2GO de données

● 1 document est limité à 16mo (vs 4 avt)

● Verrouillage global des données partagées

● Jusque version 2.0

● Meilleures performances en 2.2+

2. MongoDB

Page 18: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Plusieurs gems pour MongoDB :

● MongoId

● Mongo Ruby Driver, officiel

● MongoMapper

● ...

3. MongoId

Page 19: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Communauté active

Basé sur ActiveSupport

Validations

Requêtes grâce à Origin

Polymorphisme

Proche de ActiveRecord :

● has_many, belongs_to

3. MongoId

Page 20: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Pris en charge pour de nombreuses gems :

● Carrierwave-mongoid

● ActiveAdmin (patch)

● mongoid-rspec

● delayed_job_mongoid

● mongoid_session_store

3. MongoId

Page 21: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Pris en charge pour de nombreuses gems :

● Carrierwave-mongoid

● ActiveAdmin (patch)

● mongoid-rspec

● delayed_job_mongoid

● mongoid_session_store

3. MongoId

Page 22: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Gestion des documents

Gestion des documents « embedded »

Gestion des « extras »

● Caching

● Versionning

● Paranoia

Gestion de Map/Reduce

3. MongoId

Page 23: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Création d'une classe :

class User include Mongoid::Document include Mongoid::Timestamps

field :firstname, :type => String, :default => "" field :lastname, :type => String, :default => "" validates :lastname, :presence => true

has_and_belongs_to_many :projects, :inverse_of => :members embeds_many :tasks, :inverse_of => :userend

3. MongoId

Page 24: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Polymorphisme :class User include Mongoid::Document include Mongoid::Timestamps

embeds_many :tasks, :as => :ownerend

class Task include Mongoid::Document include Mongoid::Timestamps

embedded_in :owner, :polymorphic => trueend

3. MongoId

Page 25: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Requêtes :user = User.where(:firstname => 'Vincent')# #<User _id: 5060ea5fe77989e76b000001, created_at: 2012-09-24 23:18:55 UTC, updated_at: 2012-11-02 13:34:46 UTC, firstname: "Vincent", lastname: "Tabary">

User.countUser.all.count# => 5

user.tasks.count# => 4

Task.count# => 0

3. MongoId

Page 26: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Requêtes :User.in(firstname: ['Vincent']).union.in(firstname: ['Aymeric']).count# => 2

User.in(firstname: ['Vincent', 'Aymeric']).union.in(firstname: ['Aymeric', 'Paul']).count# => 1

3. MongoId

Page 27: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

Extras :class User include Mongoid::Document include Mongoid::Timestamps include Mongoid::Paranoia include Mongoid::Caching include Mongoid::VersionningEnd

3. MongoId

Page 28: Présentation mongoDB et mongoId

co mp ie gn e .rb

15 no v. 20 12

http://fr.wikipedia.org/wiki/NoSQL

http://www.mongodb.org/

http://rsmith.co/2012/11/05/mongodb-gotc...

https://www.ruby-toolbox.com...

6. Bibliographie


Recommended