mongoid

remove an embedded document in mongoid

Hello I have a projects model with just a name field and in it also the embedded relation to line_items. class Project include mongoid::document field :name embeds_many :line_items end class LineItem include mongoid::document field :title embedded_in :project, :inverse_of => :line_items en...

How to page embedded documents in mongo

Lets say we have a posts collection, and embedded in posts is an array of comments. How would I write a query to say, pull out only posts 11-20? A mongoid solution would be preferable, but a mongo query object would also be fine ...

How to avoid the same record inserted twice in MongoDB (using Mongoid) or ActiveRecord (Rails using MySQL)?

For example, if we are doing Analytics recording the page_type, item_id, date, pageviews, timeOnPage. It seems that they are several ways to avoid it. Is there an automatic way? create index on the fields that uniquely identify the record, for example [page_type, item_id, date] and make the index unique, so that when adding the sam...

Mongoid Object.all seems to return a lot of []

Why doesn't this work: ruby-1.8.7-p249 > List.create :search_terms => 'foo' => #<List _id: 4c9044a02249c7a5e2000001, search_terms: "foo", user_id: nil> ruby-1.8.7-p249 > List.all => #<Mongoid::Criteria:0x1030dea90 @klass=List, @documents=[], @selector={}, @options={}> ruby-1.8.7-p249 > List.all.documents => [] ...

Mongoid on Rails won't take SomeClass.where({:some_date.gte => '2010-09-01'}) if some_date is DateTime but needs to be Time class?

It is strange that if in the model: def SomeClass some_date: Time end then SomeClass.where({:some_date.gte => '2010-09-01'}) would work well, but when it is def SomeClass some_date: DateTime end (change Time to DateTime) then the query won't work? (will get 0 items back). In the MongoDB shell, they both show up as > db....

How does MongoDB compares the date only and ignores the time, such as date <= '2010-09-10' ?

For some reason: Analytic.where({:ga_date.gte => '2010-09-01'}).count() # greater than or equal to gives back 0, but Analytic.where({:ga_date.gte => Time.parse('2010-09-01')}).count() gives back 230, which is the number of records (documents). Actually, the first line on the top works in another case, so it is quite strange. Ca...

Does Mongoid have Map/Reduce?

I am using Ruby code to calculate sum from the array returned by Mongoid. But maybe using Map/Reduce can be faster, except I don't see any docs for Map Reduce on mongoid.org and Google for map reduce site:mongoid.org doesn't give any result either. (or using MapReduce or Map/Reduce) There are docs on MongoDB's site map reduce site...

Why MongoDB can create unique index but Mongoid cannot?

In the MongoDB shell, if I do the following, then an index is created, and also prevent duplicate records from being inserted: db.analytics.ensureIndex({page: 1, some_id: 1, ga_date: -1}, {unique: true}); But I thought Mongoid can do the same: http://mongoid.org/docs/indexing/ So I have: class PageAnalytic < Analytic include Mong...

How to use MongoDB Ruby Driver to do a "Group" (group by)?

related to http://stackoverflow.com/questions/2127374/mongodb-group-using-ruby-driver if I want to do something like the following in SQL: select page_id, count(page_id) from a_table group by page_id I thought the MongoDB's doc says http://api.mongodb.org/ruby/current/Mongo/Collection.html#group-instance_method group(key, condition...

find_by_sql equivalent for mongoid?

Is there some sort of find_by_sql equivalent for mongoid, where you pass a mongo query and it materializes Mongoid::Document s from the results? ...

Does MongoDB's Map/Reduce always return results in floats?

I am using Mongoid, which is on top of the Ruby MongDB driver. Even though my Map's emit is giving out a parseInt(num), and the Reduce's return is giving back also a parseInt(num), the final results still are floats. Is that particular to MongoDB? Any way to make it integer instead? ...

Recommendations on structure for Mongoid/MongoDB Tree of Tags

Hi, I'm looking for some recommendations on how to structure the tags part of this data model: Here's a simplified version of it: a Site has many Posts (relational association [references_many in mongoid speak]). A Site has a tree of tags a Post has an array of tags (subset of the Site's tags, order doesn't matter) The use cases I...

Is there a way to pass through "find" before map_reduce for MongoDB?

The following line works: Analytic.collection.map_reduce(map, reduce).find but is there a way to do Analytic.collection.find('page_type' => 'products').map_reduce(map, reduce).find and even filter a date range such as date >= "2010-08-01" and date <= "2010-08-31"? ...

In MongoDB, if an index is on 3 fields, we can use that index when querying on 2 fields? (wildcard on the 3rd field)

If there is an index on page_type, our_id, date and when querying, db.analytics.find({page_type: 'ingredients', ga_date: {$gte : new Date('Wed Sep 08 2010 12:00:00 GMT-0800')}}) db.analytics.find({page_type: 'ingredients', ga_date: {$gte : new Date('Wed Sep 08 2010 12:00:00 GMT-0800')}}).explain() if our_id is omitted, or da...

Does MongoDB's Map/Reduce sort work?

If the following is used Analytic.collection.map_reduce(map, reduce, :query => {:page => subclass_name}, :sort => [[:pageviews, Mongo::DESCENDING]]).find.to_a it won't sort by pageviews. Alternatively, if it is array of hash: Analytic.collection.map_reduce(map, reduce, :query => {:page => subclass_name}, :sort => [{:page...

Can I have thread safe per-request configuration for database connection and table_name in ActiveRecord (or Mongoid)?

Also known as the <<"User has many Databases" question.>> The environment My app is modeled like so: user has_many databases database has_many tables table has_many rows row habtm(+value) columns you get the idea! So instead of modelling a database inside a database, I would like to have: a sqlite3 database that h...

Batch insert/update using Mongoid?

I googled and all others, but I didn't find the answer. The question is: Hi, how can I do batch insert with Mongoid to MongoDB? ...

Is MongoDB's query or Mongoid's API good for filtering 2000 items from a total of 80,000?

The idea is to do analytics of 30 or 2000 products out of a collection of 80,000 products. Say, if there are 80,000 products, and to get the top products with highest number of pageviews in a category, which can include only 30 or up to 2000 products, so we can either filter out all those products first, and then use map/reduce to find ...

Using MongoDB, any easy way to re-use Map/Reduce results?

For example, when doing Analytics, there can be a map/reduce run that takes 10 seconds. After it is run, if other webpages can make use of that result, then it will be saving 10 seconds per page. It will be good to have the map/reduce result cached somehow. It is possible to record a sucessful map/reduce run as map_reduce_result_[time...

Integrate Mongoid and CanCan

Hi guys, Have somebody tried to rewrite CanCan ActiverRecordAddtions for Mongoid http://github.com/ryanb/cancan/blob/master/lib/cancan/active_record_additions.rb Regards, Alexey Zakharov ...