MongoDB with the Ruby driver
Uses the mongo gem 2.26. The code is an ordinary application for the original database; nothing in it knows about MachNoSQL.
The only change
Section titled “The only change”mongodb://app:secret@mongo.example.com:27017/shop?authSource=adminmongodb://app:secret@machnosql.example.com:27017/shop?authSource=adminThe address comes from the environment variable MONGODB_URI, so switching to MachNoSQL changes configuration only.
export MONGODB_URI='mongodb://app:secret@machnosql.example.com:27017/shop?authSource=admin'bundle installbundle exec ruby app.rbSource
Section titled “Source”The program as it is kept and tested in the Mach Database NoSQL repository; highlighted lines read the connection setting.
# Orders example for MongoDB, unchanged on MachNoSQL.require "mongo"
client = Mongo::Client.new(ENV.fetch("MONGODB_URI"))orders = client[:orders]
orders.delete_many({})orders.insert_many([ { customer: "ada", item: "keyboard", qty: 1, price: 49.0 }, { customer: "ada", item: "mouse", qty: 2, price: 19.5 }, { customer: "linus", item: "monitor", qty: 1, price: 199.0 }])
puts orders.find(item: "mouse").firstorders.update_one({ item: "mouse" }, { "$inc" => { qty: 1 } })
orders.aggregate([ { "$group" => { _id: "$customer", revenue: { "$sum" => { "$multiply" => ["$qty", "$price"] } } } }, { "$sort" => { revenue: -1 } }]).each { |row| puts row }
client.closesource "https://rubygems.org"
gem "mongo", "~> 2.26"