Introduction to using MongoDB with Ruby

764 views

Published on

Introduction to using MongoDB with Ruby

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
764
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to using MongoDB with Ruby

  1. 1. with Ruby and MongoMapper
  2. 2. 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”
  3. 3. 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)
  4. 4. 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!”
  5. 5. 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
  6. 6. 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
  7. 7. Mental Mapping Table = Collection Row = JSON Document Index = Index Join = Embedded Document Partition = Shard Partition Key = Shard Key Useful Comparison Document http://docs.mongodb.org/manual/reference/sql-comparison/
  8. 8. 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
  9. 9. Install the gems for MongoMapper gem install mongo_mapper gem 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
  10. 10. Use mongod to start the database: mongod --rest --dbpath=/home/jholloway/mongodb Starting MongoDB
  11. 11. Native Port Used by the MongoDB driver - in my case the MongoMapper gem interacts with this http://127.0.0.1:27017 Web UI Allows us to inspect the MongoDB http://127.0.0.1:28017 There's also a Mongo interactive shell (mongo) MongoDB Interfaces
  12. 12. ● 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
  13. 13. 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, Float end
  14. 14. 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()
  15. 15. ● Let's go back to the Mongo shell ./mongo show dbs use myebayitems show collections db.items.find() Mongo Shell: Raw Storage
  16. 16. 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} )
  17. 17. 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()}"
  18. 18. 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/
  19. 19. 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
  20. 20. MongoMapper: Full Text Search Bah ! You can do it! MongoMapper Search Gem - didn’t work for me https://github.com/mariopeixoto/mongomapper_search
  21. 21. MongoMapper: Plugins Fair 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
  22. 22. MongoMapper: In Retrospect I’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
  23. 23. Questions Thanks! All the code will be up here in a Github project: https://github.com/jph98/ebaymongo
  24. 24. By the way we’re hiring... ● Javascript/Java Developers ● Devops ● R Consultants Email: careers@mango-solutions.com

×