Skip to content

Instantly share code, notes, and snippets.

@slowjud
Last active August 29, 2015 13:57
Show Gist options
  • Save slowjud/9477341 to your computer and use it in GitHub Desktop.
Save slowjud/9477341 to your computer and use it in GitHub Desktop.
All the commands and seed data for my mongoid talk at RORO
# Indexes
Bike.where(style: 'FIXIE').explain['indexBounds']
Bike.where(brand_name: 'Genesis').explain['indexBounds']
Bike.where(style: 'FIXIE', brand_name: 'Genesis').explain['indexBounds']
# Nil and unset
chinarello = Bike.first
chinarello.colour = nil
chinarello.save
chinarello.reload
chinarello.as_document.keys
chinarello.unset :colour
chinarello.as_document.keys
# Nil and Exists
chinarello = Bike.first
chinarello.colour = nil
chinarello.save
Bike.exists(colour: false).count
Bike.first.unset :colour
Bike.exists(colour: false).count
# BigDecimal ordering
Bike.all.order_by(price: :asc).each{|b| puts b.price};nil
# Updating embedded objects without embedded_in decleration
chinarello = Bike.first
chinarello.parts.first.colour = 'HOT PINK'
chinarello.save
chinarello.reload
chinarello.parts.first.colour
# Indexes on embedded objects
Bike.where('parts.brand_name' => 'SRAM').explain['indexBounds']
# Embedded object callbacks
chinarello = Bike.first
chinarello.parts.first.colour = 'red'
chinarello.save
chinarello.reload
chinarello.parts.first.colour
# Two atomic operations
Bike.collection.find(brand_name: 'Chinarello').update({'$unset' => {'parts.0.brand_name' => 1}}, {'$set' => {'parts.1.colour' => 'HOT PINK'}})
Bike.where(brand_name: 'Chinarello').first.parts[1].colour
b1 = Bike.create!(style: 'CARBON_MONSTROSITY', colour: 'RED', brand_name: 'Chinarello', price: 10999.99)
b1.parts << Part.new(name: 'CRANK', colour: 'RED', brand_name: 'SRAM')
b1.parts << Part.new(name: 'HUB', colour: 'RED', brand_name: 'Chris King')
b1.parts << Part.new(name: 'PEDALS', colour: 'BLACK', brand_name: 'LOOK')
b2 = Bike.create!(style: 'DUALIE', colour: 'GREEN', brand_name: 'BMC', price: 7300)
b2.parts << Part.new(name: 'CRANK', colour: 'GOLD', brand_name: 'SRAM')
b2.parts << Part.new(name: 'HUB', colour: 'SILVER', brand_name: 'Easton')
b2.parts << Part.new(name: 'PEDALS', colour: 'BLACK', brand_name: 'Shimano')
b3 = Bike.create!(style: 'TOURER', colour: 'BROWN', brand_name: 'Salsa', price: 2300)
b3.parts << Part.new(name: 'CRANK', colour: 'SILVER', brand_name: 'Shimano')
b3.parts << Part.new(name: 'HUB', colour: 'BLACK', brand_name: 'Dt-Swiss')
b3.parts << Part.new(name: 'PEDALS', colour: 'SILVER', brand_name: 'Shimano')
b4 = Bike.create!(style: 'FIXIE', colour: 'BLUE', brand_name: 'Genesis', price: 1150)
b4.parts << Part.new(name: 'CRANK', colour: 'BLACK', brand_name: 'SRAM')
b4.parts << Part.new(name: 'HUB', colour: 'WHITE', brand_name: 'Velocity')
b4.parts << Part.new(name: 'PEDALS', colour: 'BLACK', brand_name: 'Shimano')
b5 = Bike.create!(style: 'FIXIE', colour: 'CREAM', brand_name: 'Surly', price: 1300)
b5.parts << Part.new(name: 'CRANK', colour: 'BLACK', brand_name: 'Surly')
b5.parts << Part.new(name: 'HUB', colour: 'BLACK', brand_name: 'Velocity')
b5.parts << Part.new(name: 'PEDALS', colour: 'SILVER', brand_name: 'Shimano')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment