Sie sind auf Seite 1von 24

Introduction into Mongo and Redis

the basics for people who are used to AR

What are they for?

MongoDB:

Redis

Document oriented database Queries can be written in Javascript Scalable and flexible Typical usecase: Databases that are rather schema-free

Key Value Store Own Jquery-similar selectors Scalable and flexible Typical usecase: Logging, Queues and other denormalized things

Schema - Free

Schema Free means, that you don't want to denormalize your data. It is especially useful, when you wan't to leave it to the user or the situation, what fields you want to store exactly You can also use 'schema free sql', which means, you can work around the schema in sql databases But here we refer to what is called 'NOSQL'

Persistence

Mongo-DB:

Redis:

Mongo is being persited automatically

Redis has two modes of persistence:

Snapshotting:

In certain intervals or manually you write a copy of everything in memory on the hard disk new information is appended to the logfile on disk

Append Only Log

Usage in Ruby / Rails

Mongo DB:

Redis:

Mongoid or Mongomapper as replacement for ActiveRecord

Redis Gem

Mongo Example
# with Mongomapper class Story include MongoMapper::Document key :title, String key :url, String key :slug, String key :voters, Array key :votes, Integer, :default => 0 key :relevance, Integer, :default => 0 # Cached values. key :comment_count, Integer, :default => 0 key :username, String # Note this: ids are of class ObjectId. key :user_id, ObjectId timestamps! # Relationships. belongs_to :user # Validations. validates_presence_of :title, :url, :user_id end # To find all the stories voted on by a given user: Story.all(:conditions => {:voters => @user.id})

Mongo with Mongoid


QUERY METHODS
class Artist include Mongoid::Document field :name, type: String embeds_many :instruments end class Instrument include Mongoid::Document field :name, type: String embedded_in :artist end # Find the first artist named "Syd Vicious" and create an embedded bass # document for him. Of course he'll smash it within the next few minutes, # but no worries we can delete it later. Artist.where(name: "Syd Vicious").first.tap do |artist| artist.instruments.create(name: "Bass") end

concept of embedded documents

Model.all_in Model.all_of Model.also_in Criteria#and Model.any_in Model.any_of Model.asc Model.desc Criteria#distinct Model.excludes Model.includes Model.limit Model.near Model.not_in Model.only Model.order_by Model.skip Model.where Model.without

Mongo Query Selectors


collection ~= 'table'

db.COLLECTION.find() db.users.find({age : 23}) default is AND SELECT: db.users.find({},{username : 1}) $gte, $lte, $gt, $lt, $ne : comparisons

>=, <=, >, <

db.users.find({age : {$gte : 18 }}) using $in: db.users.find({user_id {$in : [1,2]}}) db.users.find({$or : [{name : foo},{title : bar}]}) $not, $all, $any, $size, $slice regular expression: {name : /foo?/i} db.users.find({name.first : Foo, name.last : Bar}) db.foo.find({$where : this.x + this.y == 10}) db.fo.find({$where : function() {return this.x + this.y == 10;}})

OR:

others:

in embedded documents:

use javascript with $where


nesting with a 'dot'

Redis Example
$redis = Redis.new(:host => 'localhost', :port => 6379) $redis => #<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.2.2)> $redis.set('chunky', 'bacon') => "OK" $redis.get('chunky') => "bacon"

Redis Lists

Lists

RPUSH: push to the right LPUSH: push to the left LPOP: pops from the left RPOP: pops from the right BLOP: blocking left pop BRPOP: blocking right pop

KEY Operations

SET GET INCR: increments the value INCRBY: increments the value by DECR: decrements the value DECRBY: decrements the value by KEYS (pattern) : display all keys matching pattern TYPE : gives us the type of a key

Redis Example: Queuing


def enqueue(queue_name, data) $redis.sadd(queues, queue_name) $redis.rpush(queue:#{queue_name}, data.to_json) end def deque(queue_name) $redis.blpop(queue:#{queue_name},60) # pops and WAITS end def work(queue_name) while true do job = self.dequeue(queues) proces_job(job) if job end end

Introduction into Mongo and Redis

the basics for people who are used to AR

What are they for?

MongoDB:

Redis

Document oriented database Queries can be written in Javascript Scalable and flexible Typical usecase: Databases that are rather schema-free

Key Value Store Own Jquery-similar selectors Scalable and flexible Typical usecase: Logging, Queues and other denormalized things
2

Schema - Free

Schema Free means, that you don't want to denormalize your data. It is especially useful, when you wan't to leave it to the user or the situation, what fields you want to store exactly You can also use 'schema free sql', which means, you can work around the schema in sql databases But here we refer to what is called 'NOSQL'
3

Persistence

Mongo-DB:

Redis:

Mongo is being persited automatically

Redis has two modes of persistence:

Snapshotting:

In certain intervals or manually you write a copy of everything in memory on the hard disk new information is appended to the logfile on disk
4

Append Only Log

Usage in Ruby / Rails

Mongo DB:

Redis:

Mongoid or Mongomapper as replacement for ActiveRecord

Redis Gem

Mongo Example
# with Mongomapper class Story include MongoMapper::Document key :title, String key :url, String key :slug, String key :voters, Array key :votes, Integer, :default => 0 key :relevance, Integer, :default => 0 # Cached values. key :comment_count, Integer, :default => 0 key :username, String # Note this: ids are of class ObjectId. key :user_id, ObjectId timestamps! # Relationships. belongs_to :user # Validations. validates_presence_of :title, :url, :user_id end # To find all the stories voted on by a given user: Story.all(:conditions => {:voters => @user.id})

Mongo with Mongoid


QUERY METHODS
class Artist include Mongoid::Document field :name, type: String embeds_many :instruments end class Instrument include Mongoid::Document field :name, type: String embedded_in :artist end # Find the first artist named "Syd Vicious" and create an embedded bass # document for him. Of course he'll smash it within the next few minutes, # but no worries we can delete it later. Artist.where(name: "Syd Vicious").first.tap do |artist| artist.instruments.create(name: "Bass") end

concept of embedded documents

Model.all_in Model.all_of Model.also_in Criteria#and Model.any_in Model.any_of Model.asc Model.desc Criteria#distinct Model.excludes Model.includes Model.limit Model.near Model.not_in Model.only Model.order_by Model.skip Model.where Model.without

Mongo Query Selectors


collection ~= 'table'

db.COLLECTION.find() db.users.find({age : 23}) default is AND SELECT: db.users.find({},{username : 1}) $gte, $lte, $gt, $lt, $ne : comparisons

>=, <=, >, <

db.users.find({age : {$gte : 18 }}) using $in: db.users.find({user_id {$in : [1,2]}}) db.users.find({$or : [{name : foo},{title : bar}]}) $not, $all, $any, $size, $slice regular expression: {name : /foo?/i} db.users.find({name.first : Foo, name.last : Bar}) db.foo.find({$where : this.x + this.y == 10}) db.fo.find({$where : function() {return this.x + this.y == 10;}}) 8

OR:

others:

in embedded documents:

use javascript with $where


nesting with a 'dot'

Redis Example
$redis = Redis.new(:host => 'localhost', :port => 6379) $redis => #<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.2.2)> $redis.set('chunky', 'bacon') => "OK" $redis.get('chunky') => "bacon"
9

Redis Lists

Lists

RPUSH: push to the right LPUSH: push to the left LPOP: pops from the left RPOP: pops from the right BLOP: blocking left pop BRPOP: blocking right pop

10

KEY Operations

SET GET INCR: increments the value INCRBY: increments the value by DECR: decrements the value DECRBY: decrements the value by KEYS (pattern) : display all keys matching pattern TYPE : gives us the type of a key
11

Redis Example: Queuing


def enqueue(queue_name, data) $redis.sadd(queues, queue_name) $redis.rpush(queue:#{queue_name}, data.to_json) end def deque(queue_name) $redis.blpop(queue:#{queue_name},60) # pops and WAITS end def work(queue_name) while true do job = self.dequeue(queues) proces_job(job) if job end end

12

Das könnte Ihnen auch gefallen