您可以确认您可以创建数据库,将集合添加到数据库并直接从 IRB 将文档添加到集合中:
$ rvm use 2.4.1
$ rvm-prompt
$ ruby-2.4.1
$ rvm gemset create mongoid_test
$ rvm use @mongoid_test
$ gem install mongoid
$ gem list | grep mongoid
$ mongoid (7.0.2)
$ rvm-prompt
$ ruby-2.4.1@mongoid_test
$ irb
> require 'mongoid'
=> true
> Mongoid.load!('mongoid.yml', :development)
=> {"clients"=>{"default"=>{"database"=>"mongoid_test", "hosts"=>["localhost:27017"]}}}
> class LineItem
include Mongoid::Document
include Mongoid::Attributes::Dynamic
end
> item = LineItem.new
> item['cost'] = 12.00
> item['quantity'] = 3
> item['name'] = 'Protein Bars'
> item.save!
=> true
> LineItem.all.size
=> 1
> i = LineItem.first
=> #<LineItem _id: 5c552b8d496a9d0828b374b5, cost: 12.0, quantity: 3, name: "Protein Bars">
> i.fields.keys
=> ["_id"]
i.inspect_dynamic_fields
=> ["cost: 12.0", "quantity: 3", "name: \"Protein Bars\""]
打开 MongoDB shell 并确认您的数据在那里:
$ mongo
> show dbs
admin
config
local
mongoid_test
> use mongoid_test
switched to db mongoid_test
> show collections
line_items
> db.line_items.find({ cost: 12.0, quantity: 3, name: 'Protein Bars'}, {_id: 0})
{ "cost" : 12, "quantity" : 3, "name" : "Protein Bars" }
直截了当,灵活,而且非常有活力。