an introduction to mongodb and ruby

15
codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 1 MongoDB & Ruby in 10 Minutes [email protected] @tobiastrelle

Upload: tobias-trelle

Post on 26-Jan-2015

126 views

Category:

Technology


0 download

DESCRIPTION

A short introduction to MongoDB and the Ruby driver for MongoDB.

TRANSCRIPT

Page 1: An introduction to MongoDB and Ruby

codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 1

MongoDB & Ruby in 10 Minutes

[email protected] @tobiastrelle

Page 2: An introduction to MongoDB and Ruby

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)

Page 3: An introduction to MongoDB and Ruby

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

Page 4: An introduction to MongoDB and Ruby

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

Page 5: An introduction to MongoDB and Ruby

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

Page 6: An introduction to MongoDB and Ruby

codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 6

Documents

{}

{ hello: "MongoDB" }

{ foo: { bar: "baz"}, a: [1,2,3,5,8]}

Page 7: An introduction to MongoDB and Ruby

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

...

Page 8: An introduction to MongoDB and Ruby

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...

Page 9: An introduction to MongoDB and Ruby

codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 9

Ruby Driver API

Mongo

MongoClient

DB Collection Cursor

Page 10: An introduction to MongoDB and Ruby

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"]

Page 11: An introduction to MongoDB and Ruby

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"}

Page 12: An introduction to MongoDB and Ruby

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]}

Page 13: An introduction to MongoDB and Ruby

codecentric AG / Tobias Trelle / MongoDB & Ruby in 10 Minutes 13

Persistence Frameworks

Mongoidhttp://mongoid.org/

MongoMapperhttp://mongomapper.com/

Page 14: An introduction to MongoDB and Ruby

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

Page 15: An introduction to MongoDB and Ruby

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 [email protected]

twitter @tobiastrelle

www.codecentric.de

blog.codecentric.de/en/author/tobias-trelle

www.xing.com/net/mongodb-dus