Mongo db intro
-
Upload
jph98 -
Category
Technology
-
view
747 -
download
1
description
Transcript of Mongo db intro
with Ruby and MongoMapper
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”
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)
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!”
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
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
Mental Mapping
Table = CollectionRow = JSON DocumentIndex = IndexJoin = Embedded DocumentPartition = ShardPartition Key = Shard Key
Useful Comparison Documenthttp://docs.mongodb.org/manual/reference/sql-comparison/
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
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
Use mongod to start the database:
mongod --rest --dbpath=/home/jholloway/mongodb
Starting MongoDB
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
● 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
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
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()
● Let's go back to the Mongo shell
./mongo
show dbsuse myebayitemsshow collections
db.items.find()
Mongo Shell: Raw Storage
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} )
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()}"
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/
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
MongoMapper: Full Text Search
Bah !
You can do it!
MongoMapper Search Gem - didn’t work for mehttps://github.com/mariopeixoto/mongomapper_search
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
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
Questions
Thanks!
All the code will be up here in a Github project:
https://github.com/jph98/ebaymongo
By the way we’re hiring...
● Javascript/Java Developers
● Devops
● R Consultants
Email: [email protected]