Queries
‣ expressed BSON query documents
‣ very flexible
‣ relatively simple query expressions
‣ pretty close to SQL conceptually
‣ examples will follow
‣ on top: map/reduce for aggregation
Montag, 31. Mai 2010
A few words on durability
‣ MongoDB only fsyncs every <n> seconds
‣ There‘s a desaster waiting to happen!
‣ When in production, replicate!
‣ This is not as bad as it sounds.
Montag, 31. Mai 2010
> use test
switched to db test
db.quotes.save({
text: "You can observe a lot just by watching.",
from: "Yogi Berra", created_at: new Date()
});
db.quotes.save({
text: "Silence is one of the hardest arguments to refute.",
from: "Josh Billings", created_at: new Date()
});
Montag, 31. Mai 2010
db.quotes.find();
// returns all records in collection.
db.quotes.find({from: "Yogi Berra"});
{
"_id" : ObjectId("4c0022551496fc2051e93695"),
"text" : "You can observe a lot just by watching.",
"from" : "Yogi Berra",
"created_at" : "Fri May 28 2010 22:06:45 GMT+0200 (CEST)"
}
Montag, 31. Mai 2010
require 'rubygems'
require 'mongo'
db = Mongo::Connection.new.db("test")
doc = {
:text => "You can observe a lot just by watching.",
:from => "Yogi Berra",
:created_at => Time.now
}
db['quotes'].insert(doc)
db['quotes'].find.each do |row|
puts row.inspect
end
{
"_id"=>$oid4bffe2896261046e79000001,
"from"=>"Yogi Berra",
"created_at"=>Fri May 28 15:34:33 UTC 2010,
"text"=>"You can observe a lot just by watching."
}
Montag, 31. Mai 2010
require 'rubygems'
require 'mongo'
db = Mongo::Connection.new.db("test")
100.times do |i|
db['numbers'].insert({"i" => i})
end
db['numbers'].find("i" => {"$lt" => 2}).each do |row|
puts row.inspect
end
# {"_id"=>$oid4bffe4396261046f25000001, "i"=>0}
# {"_id"=>$oid4bffe4396261046f25000002, "i"=>1}
Montag, 31. Mai 2010
db = Mongo::Connection.new.db("test")
grid = Mongo::Grid.new(db)
id = grid.put("You can put Strings in here",
:filename => 'test.txt')
file = grid.get(id)
puts file.filename
puts file.read
grid.delete(id)
grid.put(
File.open("/Users/jankrutisch/Dropbox/Photos/IMGP8989.jpg")
)
Montag, 31. Mai 2010
fs = Mongo::GridFileSystem.new(db)
fs.open("test.txt", "w") do |f|
f.write "You can put stuff in here"
end
fs.open("test.txt", "r") do |f|
puts f.read
end
fs.delete("test.txt")
Montag, 31. Mai 2010
mongo_mapper
‣ By John Nunemaker (@jnunemaker)
‣ works
‣ a few quirks
‣ almost completely undocumented
‣ Some stuff is still missing
Montag, 31. Mai 2010
mongoid
‣ By Durran Jordan (Hashrocket)
‣ Two major versions:
‣ 1.x (currently 1.9) for Rails 2.3 compatibility
‣ 2.x (currently 2.x beta) for Rails 3 compatibility
‣ Good documentation
‣ API is better (?)
Montag, 31. Mai 2010
class Loop
include Mongoid::Document
include Mongoid::Timestamps
field :name
field :public, :type => Boolean
field :message_id
field :plays_and_downloads, :type => Integer
belongs_to_related :user
end
Montag, 31. Mai 2010
Criteria API
‣ A bit like Arel
‣ chainable method calls
‣ Named scopes
Montag, 31. Mai 2010
class Person
include Mongoid::Document
field :first_name
field :last_name
embeds_one :address
embeds_many :phones
end
class Address
include Mongoid::Document
field :street
field :city
field :state
field :post_code
embedded_in :person, :inverse_of => :address
end
Montag, 31. Mai 2010
class Loop
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Grid
field :name
field :public, :type => Boolean
field :message_id
field :plays_and_downloads, :type => Integer
attachment :nan
end
Montag, 31. Mai 2010