Mongoid Without Rails
Asked Answered
R

4

15

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.

Renner answered 7/3, 2013 at 23:34 Comment(0)
S
13

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.

  1. Define a mongoid.yml file with your database connection info in it like normal.
development:
  clients:
    default:
      database: mongoid
      hosts:
        - localhost:27017
  1. Make sure you've required Mongoid in your application.
  2. Call 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.

Sthenic answered 7/3, 2013 at 23:58 Comment(1)
@ashes999 the answer's correct though. :-/ It just takes a little digging / understanding about what's happening under the hood in the database below mongoid. "client" is the official MongoDB terminology you'll find their docs, and mongoid added the concept of a "session" on top of that. In modern versions of mongoid, the entire concept of the session has been done away with.Sthenic
B
4

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"
Boundary answered 1/8, 2014 at 2:24 Comment(1)
+1 your working example of mongoid.yml solved my problem of make sure you have a top-level sessions key with at least 1 default session configuration for it. The official mongo docs use clients instead of sessions, which doesn't workSharilyn
E
0

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).

Erymanthus answered 27/1, 2014 at 20:43 Comment(0)
C
0

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.

Car answered 2/2, 2019 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.