Professional Documents
Culture Documents
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:
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
Mongo DB:
Redis:
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})
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
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:
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
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:
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
Mongo DB:
Redis:
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})
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
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:
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
12