Reputation: 23576
I'm playing with a standalone ruby application and can't configure Mongoid 3.0.13 to work.
I've run across a couple example apps that have configuration blocks like:
Mongoid::Config.instance.from_hash({"database" => "oid"})
Or
Mongoid.configure do |config|
name = "mongoid_test_db"
host = "localhost"
port = 27017
config.database = Mongo::Connection.new.db(name)
end
These result in:
undefined method `database=' for Mongoid::Config:Module (NoMethodError)
It seems the configuration settings have changed recently.
I also tried:
Mongoid::Config.connect_to("sweet")
But that seems to do nothing.
Upvotes: 15
Views: 4800
Reputation: 9507
You can confirm that you can create the database, add a collection to the database and add documents to the collection directly from 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\""]
Open up the MongoDB shell and confirm your data is there:
$ 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" }
Straight forward, flexible and, well, quite dynamic.
Upvotes: 0
Reputation: 887
By "standalone" I'm assuming you mean not rails. Mongoid actually provides an easy way to make this work regardless of how you're running it.
mongoid.yml
file with your database connection info in it like normal.development:
clients:
default:
database: mongoid
hosts:
- localhost:27017
Mongoid.load!
to have Mongoid parse your configuration file and initialize itself.require 'mongoid'
Mongoid.load!('/path/to/your/mongoid.yml')
This info can also be found here under the "Sinatra, Padrino, and others" section: http://mongoid.org/en/mongoid/docs/installation.html
The same approach is applicable for non-webapps. Hope that helps.
Upvotes: 13
Reputation: 52231
Try this:
prompt> ruby myapp.rb
hello world
prompt> cat mongoid.yml
development:
sessions:
default:
database: myapp
hosts:
- localhost:27017
prompt> cat myapp.rb
require 'mongoid'
Mongoid.load!("mongoid.yml", :development)
puts "hello world"
Upvotes: 4
Reputation: 952
The previous answer is correct to use Mongoid.load! if you want to load from a mongoid config file. I ran into a case where I needed to embed the Mongoid config in another config file. Therefore, I needed a way to load the configuration from a hash.
In >3.1, you will be able to call Mongoid.load_configuration(hash).
Unfortunately, this function is private in 3.0. Therefore, setting up a public alias method before loading Mongoid works:
module Mongoid
module Config
def load_configuration_hash(settings)
load_configuration(settings)
end
end
end
Make sure this code gets called before require 'mongoid'. Now you can call Mongoid.load_configuration_hash(hash).
Upvotes: 0