More Related Content Similar to Mongodb railscamphh Similar to Mongodb railscamphh(17) More from jan_mindmatters More from jan_mindmatters(12) Mongodb railscamphh8. {
"_id" : ObjectId("4c00245062610475a005afcd"),
"address" : "Bernstorffstr. 174n22767 HamburgnDE",
"description" : null,
"position" : {
"lat" : 53.5600912,
"lng" : 9.9596977
},
"tags" : [
"hausarzt",
"naturheilverfahren",
"akupunktur",
"allgemeinmedizin"
],
"title" : "Dr. med. Lilo Eisenbarth",
"loxicon_id" : 808261
}
Samstag, 23. Oktober 2010
38. > 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()
});
Samstag, 23. Oktober 2010
39. > 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()
});
Samstag, 23. Oktober 2010
51. function(key, values) {
var total = 0;
values.forEach(function(v) { total += v.count });
return {count: total}
}
Samstag, 23. Oktober 2010
61. I‘m in u‘r rubies,
querying teh MongoDB!
Samstag, 23. Oktober 2010
74. doc = {
:text => "You can observe a lot just by watching.",
:from => "Yogi Berra",
:created_at => Time.now
}
@db['quotes'].insert(doc)
Samstag, 23. Oktober 2010
112. function(doc, prev) {
for(i in doc.tags) {
if (doc.tags[i] in prev.tags) {
prev.tags[doc.tags[i]]++
} else {
prev.tags[doc.tags[i]] =1
}
}
}
Samstag, 23. Oktober 2010
113. {"created_at"=>2010-09-19 22:00:00 UTC, "tags"=>{"foo"=>11.0, "dumb"=>12.0, "stupid"=>7.0, "bar"=>7.0, "cool"=>14.0, "weird"=>17.0}}
{"created_at"=>2010-09-20 22:00:00 UTC, "tags"=>{"dumb"=>11.0, "stupid"=>5.0, "foo"=>10.0, "cool"=>8.0, "weird"=>9.0, "bar"=>15.0}}
{"created_at"=>2010-09-22 22:00:00 UTC, "tags"=>{"weird"=>15.0, "bar"=>9.0, "stupid"=>17.0, "cool"=>11.0, "dumb"=>10.0, "foo"=>12.0}}
{"created_at"=>2010-09-15 22:00:00 UTC, "tags"=>{"foo"=>11.0, "weird"=>7.0, "stupid"=>10.0, "cool"=>11.0, "bar"=>5.0, "dumb"=>8.0}}
{"created_at"=>2010-09-25 22:00:00 UTC, "tags"=>{"dumb"=>11.0, "weird"=>14.0, "cool"=>8.0, "foo"=>21.0, "bar"=>11.0, "stupid"=>13.0}}
{"created_at"=>2010-09-28 22:00:00 UTC, "tags"=>{"cool"=>11.0, "dumb"=>16.0, "stupid"=>11.0, "weird"=>15.0, "foo"=>9.0, "bar"=>16.0}}
{"created_at"=>2010-09-10 22:00:00 UTC, "tags"=>{"dumb"=>15.0, "weird"=>9.0, "bar"=>8.0, "cool"=>14.0, "stupid"=>11.0, "foo"=>11.0}}
{"created_at"=>2010-09-03 22:00:00 UTC, "tags"=>{"weird"=>14.0, "dumb"=>15.0, "stupid"=>11.0, "foo"=>16.0, "bar"=>20.0, "cool"=>10.0}}
{"created_at"=>2010-09-21 22:00:00 UTC, "tags"=>{"weird"=>15.0, "cool"=>14.0, "foo"=>13.0, "stupid"=>6.0, "bar"=>11.0, "dumb"=>9.0}}
{"created_at"=>2010-09-23 22:00:00 UTC, "tags"=>{"weird"=>15.0, "stupid"=>15.0, "dumb"=>15.0, "foo"=>16.0, "cool"=>10.0, "bar"=>11.0}}
{"created_at"=>2010-09-29 22:00:00 UTC, "tags"=>{"bar"=>9.0, "cool"=>14.0, "weird"=>16.0, "foo"=>8.0, "dumb"=>9.0, "stupid"=>12.0}}
{"created_at"=>2010-09-27 22:00:00 UTC, "tags"=>{"cool"=>13.0, "dumb"=>10.0, "stupid"=>12.0, "bar"=>8.0, "foo"=>16.0, "weird"=>13.0}}
{"created_at"=>2010-09-04 22:00:00 UTC, "tags"=>{"cool"=>11.0, "bar"=>9.0, "stupid"=>6.0, "weird"=>11.0, "dumb"=>8.0, "foo"=>11.0}}
{"created_at"=>2010-09-08 22:00:00 UTC, "tags"=>{"stupid"=>12.0, "dumb"=>11.0, "cool"=>15.0, "foo"=>11.0, "bar"=>9.0, "weird"=>8.0}}
{"created_at"=>2010-10-02 22:00:00 UTC, "tags"=>{"bar"=>8.0, "dumb"=>8.0, "cool"=>10.0, "foo"=>10.0, "stupid"=>8.0, "weird"=>6.0}}
{"created_at"=>2010-09-24 22:00:00 UTC, "tags"=>{"foo"=>13.0, "bar"=>12.0, "stupid"=>15.0, "weird"=>17.0, "dumb"=>7.0, "cool"=>10.0}}
{"created_at"=>2010-09-30 22:00:00 UTC, "tags"=>{"bar"=>10.0, "cool"=>6.0, "stupid"=>14.0, "weird"=>9.0, "dumb"=>12.0, "foo"=>19.0}}
{"created_at"=>2010-09-05 22:00:00 UTC, "tags"=>{"dumb"=>12.0, "foo"=>19.0, "weird"=>8.0, "stupid"=>8.0, "bar"=>7.0, "cool"=>10.0}}
{"created_at"=>2010-09-17 22:00:00 UTC, "tags"=>{"weird"=>13.0, "bar"=>14.0, "dumb"=>12.0, "foo"=>12.0, "stupid"=>10.0, "cool"=>9.0}}
{"created_at"=>2010-09-18 22:00:00 UTC, "tags"=>{"dumb"=>8.0, "cool"=>11.0, "foo"=>6.0, "bar"=>12.0, "weird"=>7.0, "stupid"=>6.0}}
{"created_at"=>2010-09-09 22:00:00 UTC, "tags"=>{"weird"=>11.0, "dumb"=>9.0, "foo"=>6.0, "bar"=>11.0, "cool"=>11.0, "stupid"=>6.0}}
{"created_at"=>2010-09-13 22:00:00 UTC, "tags"=>{"dumb"=>19.0, "stupid"=>9.0, "weird"=>12.0, "cool"=>11.0, "bar"=>10.0, "foo"=>15.0}}
{"created_at"=>2010-09-16 22:00:00 UTC, "tags"=>{"bar"=>6.0, "weird"=>8.0, "dumb"=>9.0, "cool"=>11.0, "stupid"=>17.0, "foo"=>15.0}}
{"created_at"=>2010-09-11 22:00:00 UTC, "tags"=>{"foo"=>10.0, "weird"=>9.0, "bar"=>8.0, "cool"=>4.0, "dumb"=>8.0, "stupid"=>9.0}}
{"created_at"=>2010-09-26 22:00:00 UTC, "tags"=>{"dumb"=>15.0, "weird"=>6.0, "stupid"=>15.0, "bar"=>10.0, "foo"=>13.0, "cool"=>15.0}}
{"created_at"=>2010-10-01 22:00:00 UTC, "tags"=>{"cool"=>7.0, "weird"=>11.0, "stupid"=>11.0, "bar"=>14.0, "foo"=>12.0, "dumb"=>11.0}}
{"created_at"=>2010-09-12 22:00:00 UTC, "tags"=>{"bar"=>7.0, "weird"=>12.0, "stupid"=>11.0, "cool"=>10.0, "foo"=>11.0, "dumb"=>9.0}}
{"created_at"=>2010-09-14 22:00:00 UTC, "tags"=>{"dumb"=>8.0, "foo"=>15.0, "cool"=>15.0, "stupid"=>15.0, "bar"=>7.0, "weird"=>14.0}}
{"created_at"=>2010-09-07 22:00:00 UTC, "tags"=>{"dumb"=>10.0, "cool"=>7.0, "foo"=>14.0, "weird"=>15.0, "bar"=>11.0, "stupid"=>7.0}}
{"created_at"=>2010-09-06 22:00:00 UTC, "tags"=>{"dumb"=>7.0, "bar"=>11.0, "cool"=>16.0, "weird"=>14.0, "foo"=>12.0, "stupid"=>6.0}}
Samstag, 23. Oktober 2010
114. {"created_at"=>2010-09-19 22:00:00 UTC, "tags"=>{"foo"=>11.0, "dumb"=>12.0, "stupid"=>7.0, "bar"=>7.0, "cool"=>14.0, "weird"=>17.0}}
{"created_at"=>2010-09-20 22:00:00 UTC, "tags"=>{"dumb"=>11.0, "stupid"=>5.0, "foo"=>10.0, "cool"=>8.0, "weird"=>9.0, "bar"=>15.0}}
{"created_at"=>2010-09-22 22:00:00 UTC, "tags"=>{"weird"=>15.0, "bar"=>9.0, "stupid"=>17.0, "cool"=>11.0, "dumb"=>10.0, "foo"=>12.0}}
{"created_at"=>2010-09-15 22:00:00 UTC, "tags"=>{"foo"=>11.0, "weird"=>7.0, "stupid"=>10.0, "cool"=>11.0, "bar"=>5.0, "dumb"=>8.0}}
{"created_at"=>2010-09-25 22:00:00 UTC, "tags"=>{"dumb"=>11.0, "weird"=>14.0, "cool"=>8.0, "foo"=>21.0, "bar"=>11.0, "stupid"=>13.0}}
{"created_at"=>2010-09-28 22:00:00 UTC, "tags"=>{"cool"=>11.0, "dumb"=>16.0, "stupid"=>11.0, "weird"=>15.0, "foo"=>9.0, "bar"=>16.0}}
{"created_at"=>2010-09-10 22:00:00 UTC, "tags"=>{"dumb"=>15.0, "weird"=>9.0, "bar"=>8.0, "cool"=>14.0, "stupid"=>11.0, "foo"=>11.0}}
{"created_at"=>2010-09-03 22:00:00 UTC, "tags"=>{"weird"=>14.0, "dumb"=>15.0, "stupid"=>11.0, "foo"=>16.0, "bar"=>20.0, "cool"=>10.0}}
{"created_at"=>2010-09-21 22:00:00 UTC, "tags"=>{"weird"=>15.0, "cool"=>14.0, "foo"=>13.0, "stupid"=>6.0, "bar"=>11.0, "dumb"=>9.0}}
{"created_at"=>2010-09-23 22:00:00 UTC, "tags"=>{"weird"=>15.0, "stupid"=>15.0, "dumb"=>15.0, "foo"=>16.0, "cool"=>10.0, "bar"=>11.0}}
{"created_at"=>2010-09-29 22:00:00 UTC, "tags"=>{"bar"=>9.0, "cool"=>14.0, "weird"=>16.0, "foo"=>8.0, "dumb"=>9.0, "stupid"=>12.0}}
{"created_at"=>2010-09-27 22:00:00 UTC, "tags"=>{"cool"=>13.0, "dumb"=>10.0, "stupid"=>12.0, "bar"=>8.0, "foo"=>16.0, "weird"=>13.0}}
{"created_at"=>2010-09-04 22:00:00 UTC, "tags"=>{"cool"=>11.0, "bar"=>9.0, "stupid"=>6.0, "weird"=>11.0, "dumb"=>8.0, "foo"=>11.0}}
{"created_at"=>2010-09-08 22:00:00 UTC, "tags"=>{"stupid"=>12.0, "dumb"=>11.0, "cool"=>15.0, "foo"=>11.0, "bar"=>9.0, "weird"=>8.0}}
{"created_at"=>2010-10-02 22:00:00 UTC, "tags"=>{"bar"=>8.0, "dumb"=>8.0, "cool"=>10.0, "foo"=>10.0, "stupid"=>8.0, "weird"=>6.0}}
{"created_at"=>2010-09-24 22:00:00 UTC, "tags"=>{"foo"=>13.0, "bar"=>12.0, "stupid"=>15.0, "weird"=>17.0, "dumb"=>7.0, "cool"=>10.0}}
{"created_at"=>2010-09-30 22:00:00 UTC, "tags"=>{"bar"=>10.0, "cool"=>6.0, "stupid"=>14.0, "weird"=>9.0, "dumb"=>12.0, "foo"=>19.0}}
{"created_at"=>2010-09-05 22:00:00 UTC, "tags"=>{"dumb"=>12.0, "foo"=>19.0, "weird"=>8.0, "stupid"=>8.0, "bar"=>7.0, "cool"=>10.0}}
{"created_at"=>2010-09-17 22:00:00 UTC, "tags"=>{"weird"=>13.0, "bar"=>14.0, "dumb"=>12.0, "foo"=>12.0, "stupid"=>10.0, "cool"=>9.0}}
{"created_at"=>2010-09-18 22:00:00 UTC, "tags"=>{"dumb"=>8.0, "cool"=>11.0, "foo"=>6.0, "bar"=>12.0, "weird"=>7.0, "stupid"=>6.0}}
{"created_at"=>2010-09-09 22:00:00 UTC, "tags"=>{"weird"=>11.0, "dumb"=>9.0, "foo"=>6.0, "bar"=>11.0, "cool"=>11.0, "stupid"=>6.0}}
{"created_at"=>2010-09-13 22:00:00 UTC, "tags"=>{"dumb"=>19.0, "stupid"=>9.0, "weird"=>12.0, "cool"=>11.0, "bar"=>10.0, "foo"=>15.0}}
{"created_at"=>2010-09-16 22:00:00 UTC, "tags"=>{"bar"=>6.0, "weird"=>8.0, "dumb"=>9.0, "cool"=>11.0, "stupid"=>17.0, "foo"=>15.0}}
{"created_at"=>2010-09-11 22:00:00 UTC, "tags"=>{"foo"=>10.0, "weird"=>9.0, "bar"=>8.0, "cool"=>4.0, "dumb"=>8.0, "stupid"=>9.0}}
{"created_at"=>2010-09-26 22:00:00 UTC, "tags"=>{"dumb"=>15.0, "weird"=>6.0, "stupid"=>15.0, "bar"=>10.0, "foo"=>13.0, "cool"=>15.0}}
{"created_at"=>2010-10-01 22:00:00 UTC, "tags"=>{"cool"=>7.0, "weird"=>11.0, "stupid"=>11.0, "bar"=>14.0, "foo"=>12.0, "dumb"=>11.0}}
{"created_at"=>2010-09-12 22:00:00 UTC, "tags"=>{"bar"=>7.0, "weird"=>12.0, "stupid"=>11.0, "cool"=>10.0, "foo"=>11.0, "dumb"=>9.0}}
{"created_at"=>2010-09-14 22:00:00 UTC, "tags"=>{"dumb"=>8.0, "foo"=>15.0, "cool"=>15.0, "stupid"=>15.0, "bar"=>7.0, "weird"=>14.0}}
{"created_at"=>2010-09-07 22:00:00 UTC, "tags"=>{"dumb"=>10.0, "cool"=>7.0, "foo"=>14.0, "weird"=>15.0, "bar"=>11.0, "stupid"=>7.0}}
{"created_at"=>2010-09-06 22:00:00 UTC, "tags"=>{"dumb"=>7.0, "bar"=>11.0, "cool"=>16.0, "weird"=>14.0, "foo"=>12.0, "stupid"=>6.0}}
"tags" => {
"foo" => 11.0,
"dumb" => 12.0,
"stupid" => 7.0,
"bar" => 7.0,
"cool" => 14.0,
"weird" => 17.0
}
Samstag, 23. Oktober 2010
115. function(prev) {
var mostPopular = 0;
for(i in prev.tags) {
if(prev.tags[i] > mostPopular) {
prev.tag = i;
prev.count = prev.tags[i];
mostPopular = prev.tags[i];
}
}
delete prev.tags
}
Samstag, 23. Oktober 2010
116. {"created_at"=>2010-09-27 22:00:00 UTC, "tag"=>"stupid", "count"=>18.0}
{"created_at"=>2010-09-29 22:00:00 UTC, "tag"=>"stupid", "count"=>20.0}
{"created_at"=>2010-09-12 22:00:00 UTC, "tag"=>"cool", "count"=>11.0}
{"created_at"=>2010-09-04 22:00:00 UTC, "tag"=>"stupid", "count"=>12.0}
{"created_at"=>2010-09-21 22:00:00 UTC, "tag"=>"stupid", "count"=>16.0}
{"created_at"=>2010-09-03 22:00:00 UTC, "tag"=>"foo", "count"=>15.0}
{"created_at"=>2010-09-26 22:00:00 UTC, "tag"=>"foo", "count"=>17.0}
{"created_at"=>2010-09-18 22:00:00 UTC, "tag"=>"foo", "count"=>17.0}
{"created_at"=>2010-09-24 22:00:00 UTC, "tag"=>"cool", "count"=>11.0}
Samstag, 23. Oktober 2010
118. map = <<-END
function() {
this.tags.forEach(function(z) {
emit(z, {count: 1});
});
}
END
reduce = <<-END
function(key, values) {
var total = 0;
values.forEach(function(v) { total += v.count });
return {count: total}
}
END
collection = @db['people'].map_reduce(
map, reduce
)
Samstag, 23. Oktober 2010
125. grid = Mongo::Grid.new(@db)
id = grid.put("You can put Strings in here",
:filename => 'test.txt')
file = grid.get(id)
file.filename
file.read
grid.delete(id)
grid.put(
File.open("/Users/jankrutisch/Dropbox/Photos/IMGP8989.jpg")
)
Samstag, 23. Oktober 2010
133. {
"cursor"=>"BtreeCursor address.city_1 multi",
"nscanned"=>1000,
"nscannedObjects"=>39,
"n"=>39, "millis"=>1,
"indexBounds"=>{
"address.city"=>[["", {}], [/haven/, /haven/]]
},
"allPlans"=>[
{
"cursor"=>"BtreeCursor address.city_1 multi",
"indexBounds"=>{
"address.city"=>[["", {}], [/haven/, /haven/]]
}
}
]
}
Samstag, 23. Oktober 2010
146. Quote.where(:from => 'Yogi Berra').all
Quote.where(:from => 'Yogi Berra').limit(5).sort(:from.desc).all
Samstag, 23. Oktober 2010
152. class Person
scope :tagged, lambda { |tag| where(:tags.in => [tag]) }
end
puts Person.tagged('cool').first.inspect
Samstag, 23. Oktober 2010
166. Quote.where(:from => 'Yogi Berra').all
Quote.where(:from => 'Yogi Berra').limit(5).order_by(:from.desc).all
Samstag, 23. Oktober 2010
168. class Person
include Mongoid::Document
field :name
embeds_one :address
field :tags, :type => Array
end
class Address
include Mongoid::Document
field :street
field :city
field :country
field :zip
end
Samstag, 23. Oktober 2010
171. class Person
scope :tagged, lambda { |tag| where(:tags.in => [tag]) }
end
puts Person.tagged('cool').first.inspect
Samstag, 23. Oktober 2010
182. Other not so noteworthy
libraries
Samstag, 23. Oktober 2010
184. thanks for listening.
‣ jan@krutisch.de
‣ http://jan.krutisch.de/
‣ http://github.com/halfbyte/
‣ http://twitter.com/halfbyte
‣ http://www.mindmatters.de/
‣ http://www.mongodb.org/
‣ http://www.mongoid.org/
‣ http://github.com/jnunemaker/mongo_mapper
‣ http://github.com/halfbyte/mongo_ruby_examples
Samstag, 23. Oktober 2010