an introduction to mongodb and ruby
Post on 26-Jan-2015
113 Views
Preview:
DESCRIPTION
TRANSCRIPT
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 1
MongoDB & Ruby in 10 Minutes
Tobias.Trelle@codecentric.de @tobiastrelle
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 2
Tobias Trelle
- Senior IT Consultant @codecentric AG (Düsseldorf)
- Organizer of MongoDBUser Group Düsseldorf
- Authoring a MongoDB book(dpunkt-Verlag)
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 3
MongoDB
- NoSQL data store / Open Source
- Single records are called documents
- Replication & Sharding out-of-the-box
- Map/Reduce
- Geospatial Indexes / Queries
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 4
MongoDB Overview
Server
Database
Collection
Document
Field
Tabelle
Zeile
Spalte
RelationalesPendant Aber …
Flexibles Schema
- Arrays- Rekursiv
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 5
Documents
- Ordered set of key/values pairs
- Values can be an array, document or simple type
- Max size = 16 MB
- Logical namespaces for documents are called collections
- Schemaless
- Format: BSON = Binary JSON != JSON
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 6
Documents
{}
{ hello: "MongoDB" }
{ foo: { bar: "baz"}, a: [1,2,3,5,8]}
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 7
MongoDB Gems
$ gem install mongo
Fetching: bson-1.9.0.gem (100%)
Fetching: mongo-1.9.0.gem (100%)
Successfully installed bson-1.9.0
Successfully installed mongo-1.9.0
2 gems installed
...
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 8
MongoDB Gems
$ gem install bson_ext
Fetching: bson_ext-1.9.0.gem (100%)
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...
Successfully installed bson_ext-1.9.0
1 gem installed...
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 9
Ruby Driver API
Mongo
MongoClient
DB Collection Cursor
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 10
Ruby Driver
irb(main):001:0> require 'mongo'
=> true
irb(main):002:0> include Mongo
=> Object
irb(main):003:0> client = MongoClient.new
...
irb(main):004:0> db = client["test"]
...
irb(main):005:0> coll = db["foo"]
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 11
Ruby Driver
irb(main):006:0> coll.remove
=> {"n"=>3, "connectionId"=>2, "err"=>nil, "ok"=>1.0}
irb(main):007:0> coll.insert :hello => "MongoDB"
=> BSON::ObjectId('51d1100ec9f0b10b78000001')
irb(main):008:0> doc = coll.find_one
=> {"_id"=>BSON::ObjectId('51d1100ec9f0b10b78000001'), "hello"=>"MongoDB"}
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 12
Ruby Driver
irb(main):013:0> coll.insert :a => 1, b: [1,2,3,4,5]
=> BSON::ObjectId('51d1108ac9f0b10b78000002')
irb(main):014:0> crsr = coll.find
=> <Mongo::Cursor:0x158e2a8 namespace='test.foo' @selector={} @cursor_id=>
irb(main):016:0> crsr.each{ |doc| puts doc }
{"_id"=>BSON::ObjectId('51d1100ec9f0b10b78000001'), "hello"=>"MongoDB"}
{"_id"=>BSON::ObjectId('51d1108ac9f0b10b78000002'), "a"=>1, "b"=>[1, 2, 3, 4, 5]}
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 13
Persistence Frameworks
Mongoidhttp://mongoid.org/
MongoMapperhttp://mongomapper.com/
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 14
MongoDB User Group Düsseldorfhttps://www.xing.com/net/mongodb-dus@MongoDUS
Meetup #7: Schema Design w/ MongoDB18.07.2013 19:00https://
www.xing.com/events/7-treffen-schema-design-mongodb-1268228
MUG Düsseldorf
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 15
QUESTIONS?
Tobias Trelle
codecentric AGMerscheider Str. 142699 Solingen
tel +49 (0) 212.233628.47fax +49 (0) 212.233628.79mail Tobias.Trelle@codecentric.de
twitter @tobiastrelle
www.codecentric.de
blog.codecentric.de/en/author/tobias-trelle
www.xing.com/net/mongodb-dus
top related