Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MongoDB & Rails

Amaia Castro
February 28, 2013

MongoDB & Rails

Intro to using MongoDB with Rails. Presented at Madrid-rb

Amaia Castro

February 28, 2013
Tweet

Other Decks in Programming

Transcript

  1. Tags posts tags SQL MongoDB posts_tags ------------ post_id tag_id {

    title: "MongoDB", tags: [ "mongodb", "madrid-rb", "ruby" ] } posts class Post include Mongoid:: Document field :title field :tags, type: Array end
  2. class Invoice include Mongoid::Document field :number, type: String embeds_many :invoice_lines

    end class InvoiceLine include Mongoid::Document field :description, type: String field :price, type: BigDecimal embedded_in :invoice end
  3. Users & Groups users groups SQL MongoDB groups_users ------------ user_id

    group_id { _id: "abc123", username: "amaia", groups: [ "654dbd", ... ] } { _id: "654dbd", name: "madrid- rb", users: [ "abc123", ... ] } users groups
  4. class PersonalProfile include Mongoid::Document field :user_id, :type => Integer field

    :name, :type => String field :interests, :type => Array field :skills, :type => Array def user User.find_by_id(self.user_id) end end class User < ActiveRecord::Base def personal_profile PersonalProfile.where(user_id: self.id).first end end
  5. • No joins or transactions at DB level • Model

    your data as independent documents • Avoid referenced associations in Mongoid • Mix and match SQL and NoSQL when needed Summary