Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

Introduction to using MongoDB with Ruby

823 visualizaciones

Publicado el

Introduction to using MongoDB with Ruby

Publicado en: Tecnología
  • Sé el primero en comentar

  • Sé el primero en recomendar esto

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)
  7. 7. Mental Mapping Table = Collection Row = JSON Document Index = Index Join = Embedded Document Partition = Shard Partition Key = Shard Key Useful Comparison Document
  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 ( source "" 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 Web UI Allows us to inspect the MongoDB 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 => "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
  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
  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:
  24. 24. By the way we’re hiring... ● Javascript/Java Developers ● Devops ● R Consultants Email: