Mongo db intro

24
with Ruby and MongoMapper

description

 

Transcript of Mongo db intro

Page 1: Mongo db intro

with Ruby and MongoMapper

Page 2: Mongo db intro

What's the presentation about?

To provide a basic introduction to MongoDB

An introduction to the MongoMapper Ruby gem and how easy, readable the code is for persisting/querying MongoDB

Caveat - “I’ve spent a few weeks learning about this with Ruby and MongoMapper”

Page 3: Mongo db intro

What the heck is MongoDB?

JSON (actually BSON - binary) based datastore with 16MB document limit

Written in C++

Distros on Linux (RPM), OSX (Brew)

Lots of different language drivers available (Ruby, R, Erlang, Python)

Page 4: Mongo db intro

Why would I use it?

Simplicity - Building Javascript applications without the layers of translation between presentation -> business logic -> data access

Scaling out - rapidly without the hassle of sharded MySQL or Postgres with Slony. 100GB+ on a single instance.

“It's quick to develop against - this is what I want to use it for, prototyping!”

Page 5: Mongo db intro

What’s Similar

CouchDB - although your datastore has to be hardcoded up front in terms of views.

● It’s hard to do query filtering etc...

● Have to query through MapReduce JS

Page 6: Mongo db intro

Who Uses MongoDB?

● Craigslist (Everybody uses it in North America instead of Ebay)

● SAP - SaaS based platform● MTV● Sourceforge - backend storage● Firebase (Datastore as a Service)

https://www.firebase.com

Page 7: Mongo db intro

Mental Mapping

Table = CollectionRow = JSON DocumentIndex = IndexJoin = Embedded DocumentPartition = ShardPartition Key = Shard Key

Useful Comparison Documenthttp://docs.mongodb.org/manual/reference/sql-comparison/

Page 8: Mongo db intro

There's lots of API's including a native MongoDB REST API, but...

I'm going to use:

● Ruby 1.9

● MongoMapper Gem

● ruby-mongo-driver (well the gem is)

Simple Storage Example

Page 9: Mongo db intro

Install the gems for MongoMapper

gem install mongo_mappergem install bson_ext

or put the following in your Gemfile and use bundler (http://bundler.io/):

source "https://rubygems.org"gem "mongo_mapper"gem "mongomapper_search"

Pre-requisites

Page 10: Mongo db intro

Use mongod to start the database:

mongod --rest --dbpath=/home/jholloway/mongodb

Starting MongoDB

Page 11: Mongo db intro

Native PortUsed by the MongoDB driver - in my case the MongoMapper gem interacts with this

http://127.0.0.1:27017

Web UIAllows us to inspect the MongoDB

http://127.0.0.1:28017

There's also a Mongo interactive shell (mongo)

MongoDB Interfaces

Page 12: Mongo db intro

● I've got a number of ebay items I want to store, specifically some retro computer games I’m tracking prices of on Ebay

Overview

Page 13: Mongo db intro

MongoMapper: Create a Document

Specify the ebay item definition using the MongoMapper API

class Item include MongoMapper::Document

key :name, String, :required => true key :location, String key :price, Floatend

Page 14: Mongo db intro

MongoMapper: Save Document

Create a new item (n.b. use of Ruby symbols) and call save on it

Item.new(:name => "Rescue from Fractalus", :platform => "Commodore 64", :location => "Chippenham", :price => 2.50).save()

Page 15: Mongo db intro

● Let's go back to the Mongo shell

./mongo

show dbsuse myebayitemsshow collections

db.items.find()

Mongo Shell: Raw Storage

Page 16: Mongo db intro

MongoMapper: Querying Documents

Will use irb for the examples here

irb -r ebayitemsave.rb

Find all items in the collection Item:

items = Item.all()

Query all items by price > 5

items = Item.all( :price => {:$gt => 5} )

Page 17: Mongo db intro

MongoMapper: Dynamic Finders

I can also use dynamic finders using the fields defined on my document, awesomeness.

So in the previous example find by price...

Item.find_by_price(2.50)puts "Item: #{item.to_mongo()}"

Page 18: Mongo db intro

MongoMapper: Embedded Docs

Joins - deeply nested JSON documents can be a performance issue though

Need to think about database design carefully up-front and model it as you’d expect to query it

This is very different from a relational database with a reporting backend

http://mongomapper.com/documentation/

Page 19: Mongo db intro

MongoMapper: MapReduce Example

MongoDB provides an aggregation framework for simple operations

It also provides a mapReduce() command which you can pass Javascript (yes) to the embedded V8 engine

n.b. Dispatches the command to each shard in a sharded MongoDB setup

Page 20: Mongo db intro

MongoMapper: Full Text Search

Bah !

You can do it!

MongoMapper Search Gem - didn’t work for mehttps://github.com/mariopeixoto/mongomapper_search

Page 21: Mongo db intro

MongoMapper: PluginsFair few of them and you can write your own easily enough:

● Associations● Accessible● Callbacks● Dirty● Keys● Modifiers● Protected● Scopes● Serialization● Single Collection Inheritance● Timestamps● Userstamps● Validations

Page 22: Mongo db intro

MongoMapper: In RetrospectI’ve just broken the surface with the capabilities of it in the past few weeks and here

Lots more investigation required into the aggregation framework and the map reduce functionality

But it’s much much better than CouchDB for what I wanted to do

Page 23: Mongo db intro

Questions

Thanks!

All the code will be up here in a Github project:

https://github.com/jph98/ebaymongo

Page 24: Mongo db intro

By the way we’re hiring...

● Javascript/Java Developers

● Devops

● R Consultants

Email: [email protected]