mongoid

After MongoDB's Map/Reduce results is saved as a permanent collection, how do you sort it internally, and get it back using Mongoid?

Is it true that Map/Reduce results can be stored permanently, but not sorted? For example, coll = Analytic.collection.map_reduce(map, reduce, :out => 'analyticsCachedResult') the above is stored permanently but is not sorted. To sort it on the fly, it can be coll.find({}, :sort => ['value.pa...

How to tell whether a collection exists in MongoDB using Mongoid?

Since Mongoid.master.collection() returns a collection even if the collection doesn't exist, we can use coll = Mongoid.master.collection('analyticsCachedResult') if coll.count == 0 # [...] end to test if it is an empty collection. Another method is to loop through Mongoid.master.collections.each do |c| return c if c.name == 'ana...

MongoDB using db.things.find("j != 3 && k > 10") vs db.things.find({j: {$ne: 3}, k: {$gt: 10} })

I have been using MongoDB and the Ruby driver and Mongoid, and lines db.things.find({j: {$ne: 3}, k: {$gt: 10} }); just seem so weird and hard to remember. Why not use a parser: db.things.find("j != 3 && k > 10") which can automatically convert to its desired form? (or whatever form it uses internally). ...

Mongoid named scope comparing two time fields in the same document

I need to create a named scope in Mongoid that compares two Time fields within the same document. Such as scope :foo, :where => {:updated_at.gt => :checked_at} This obviously won't work as it treats :checked_at as a symbol, not the actual field. Any suggestions on how this can be done? Update 1 Here is my model where I have this scop...

what is the equivalent of ActiveRecord#establish_connection in Mongoid ?

Table is a Mongoid model that must dinamically map to different databases/tables # app/models/table.rb class Table include Mongoid::Document end # in app/controllers/some_controller.rb def index Table.connection.database = :other_database # <- How to do this ??? Table.table_name = params[:id] # <- How to do this ??? @records...

Mongo DB Design, embedding vs relationships

I'm building a simple accounting system where a user has many bills. Now I'm trying to decide if bills should be its own collection, or nested within the user. I'm leaning towards the former but I've NEVER done any noSQL stuff so I'm just going by trial and error and what I think makes sense to me. I understand that Mongo has a 4mb ...

money representation in mongoid

How should I work with Money with MongoID? Should I configure it as BigDecimal? And at rails level? For ActiveRecord we have something called Money, but AFAIK it just supports AR ...

mongoid relational polymorphic association

hello, does anyone know how to do a polymorphic association in mongoid that is of the relational favor but not the embedding one. For instance class Assignment include Mongoid::Document include Mongoid::Timestamps field :user field :due_at, :type => Time referenced_in :assignable, :inverse_of =>...

about embedded document referencing many other documents

I have a following association Class Person include Mongoid::Document embeds_many :employments end Class Employment include Mongoid::Document references_many :centres end class Centre include Mongoid::Document referenced_in :employment end Now when I tried Person.first.employments.first.centres.build it gav...

How do you write "(A OR B) AND (A OR C)" queries with Mongoid?

I need to write a MongoDB query of the form "A OR B OR (C AND D)" to return some records. We are using Mongoid for data access to our models. I expanded that query to "(A OR B OR C) AND (A OR B OR D)" and was hoping that using Mongoid's Criteria method any_of like this: Model.any_of(A, B, C).any_of(A, B, D) would accomplish what I want...

Sinatra Mongoid String not valid UTF-8

Hi, I wrote this little application : require 'rubygems' require 'sinatra' require 'bson' require 'mongoid' Mongoid.configure do |config| name = "articles" host = "localhost" config.master = Mongo::Connection.new.db(name) config.persist_in_safe_mode = false end class Article include Mongoid::Document field :title field...

How to reference an embedded document in Mongoid?

Using Mongoid, let's say I have the following classes: class Map include Mongoid::Document embeds_many :locations end class Location include Mongoid::Document field :x_coord, :type => Integer field :y_coord, :type => Integer embedded_in :map, :inverse_of => :locations end class Player include Mongoid::Document ...

Mongo Design question with Rails/Mongoid for a bill tracking app

I'm writing a quick app for a user to track their daily bills (for money tracking purposes). I want the user to be able to define their own categories that a bill can be applicable for. I'm trying however to decide the best way to model this and also validate categories as unique. My initial thought was this: class User include Mon...

N+1 problem in mongoid

I'm using Mongoid to work with MongoDB in Rails. What I'm looking for is something like active record include. Currently I failed to find such method in mongoid orm. Anybody know how to solve this problem in mongoid or perhaps in mongomapper, which is known as another good alternative. ...

Restrict embedded docs in query with Mongoid

I'm writing a quick little money tracking rails app for fun that allows a user to enter their daily bills. I have a user embeds_many :bills. I'm using Devise for auth. Here's my question. When I fetch current_user each time from my session_id, it's going to pull in my whole user and its embedded docs correct? So as bills add up, my ...

activerecord query as map reduce in mongodb

I would like to know how I can reproduce the following query as map reduce or any juice function from MongoMapper or MongoID: Event.find(:all,:select=>'events.company,SUM(price) as total_price', :group=> 'company',:order => ' total_price DESC, created_at DESC') ...

mapping to value in deep hash

Hi all, I'm doing an application using mongodb and mongoid and I'm facing a problem where I need to map something in one document to something in another document. My plan is to store something in a document that I can then use to figure out what value for it to fetch from a different collection. But, this is more generally a ruby quest...

Mongoid / Mongodb and querying embedded documents

Hi, I have an Author model and a Book model. When the Author has many embedded Books, is it possible to query for all Books in mongoid (rails 3.0.1) or do I always have to fetch the Author to get the Books? Best regards, sewid ...

DateTime with MongoDB/Mongoid and Rails 3 Not Populating

Here is the code in my Model include Mongoid::Document include Mongoid::Timestamps field :message, :type => String field :send_at, :type => DateTime Here is the code for my form partial <%= f.label :send_at %><br /> <%= f.datetime_select :send_at %> But the date and time is never populated. I made sure that Mongo and Mongoi...

Defining models with Mongoid for this database structure

Hey I'm trying to build a rails 3 app with Mongoid (for MongoDB). What I'm now trying to do: Languages: id (automatically created, right?) name (e.g. English) code (e.g. en_US) Languages_Texts: id (see above...) name (e.g. hello_world) Translations: id (see above...) translation (e.g. Hello, world!) I hope this database sc...