Rails, Mongoid & Unicorn config for Heroku

You dont actually need to do this, so for people coming to view this question see: http://mongoid.org/en/mongoid/docs/rails.html “Unicorn and Passenger When using Unicorn or Passenger, each time a child process is forked when using app preloading or smart spawning, Mongoid will automatically reconnect to the master database. If you are doing this in your application … Read more

Mongoid / Mongodb and querying embedded documents

You can query embedded documents, just qualify the name. Now, this will return all Authors that have books that match your query. If Author is defined as having many :books (and book is an embedded::document) @authors_with_sewid = Author.where(“books.name” => “sewid”).all You’d then need to iterate over the authors and extract the books.

Retrieving array of ids in Mongoid

Remember that the ID is stored as :_id and not :id . There is an id helper method, but when you do queries, you should use :_id: User.where(:_id.in => arr) Often I find it useful to get a list of ids to do complex queries, so I do something like: user_ids = User.only(:_id).where(:foo => :bar).distinct(:_id) … Read more

How to query MongoDB directly from Ruby instead of using Mongoid?

If you’re using Mongoid 3, it provides easy access to its MongoDB driver: Moped. Here’s an example of accessing some raw data without using Models to access the data: db = Mongoid::Sessions.default # inserting a new document collection = db[:collection_name] collection.insert(name: ‘my new document’) # finding a document doc = collection.find(name: ‘my new document’).first # … Read more

Mongo ids leads to scary URLs

You can create a composite key in mongoid to replace the default id using the key macro: class Person include Mongoid::Document field :first_name field :last_name key :first_name, :last_name end person = Person.new(:first_name => “Syd”, :last_name => “Vicious”) person.id # returns “syd-vicious” If you don’t like this way to do it, check this gem: https://github.com/hakanensari/mongoid-slug

How to deal with the timezone issue when storing dates in utc using mongod?

Aside from the SERVER-6310 mentioned by Matt Johnson, one other workaround is to use the $project operator to add or subtract from the UTC time zone to “shift the time” into the correct local zone. Turns out you can add or subtract time in milliseconds. For example, assuming I have a Date field called orderTime. … Read more

Mongoid 3 + Heroku (MongoHQ) cause Moped::Errors::OperationFailure

You will need to use Ruby 1.9.3. See Mongoid documentation here and Heroku documentation here on how to use 1.9.3. I was also getting similar errors, and I changed my gemfile to include ruby ‘1.9.3’ and everything worked as expected again. Edit: @herb pointed out that you need the latest version of bundler for this … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)