mongoid

OR query matching nil or “” with Mongoid still matches “”?

无人久伴 提交于 2019-12-24 01:59:36
问题 I'm trying to write a query for an embedded Mongoid::Document which finds any record where the "address" field is neither nil nor "". Using a combination of the MongoDB documentation, this issue in the Mongoid bug reports, and the Mongoid documentation, I think that something like this should work: scope :with_address, where("$or" => [{:address => {"$ne" => nil}}, {:address => {"$ne" => ""}}]) When I run this, the selector looks ok: 1.9.2p290 :002 > report.document.records.with_address => #

Why do I get an error installing bson_ext?

醉酒当歌 提交于 2019-12-24 00:57:05
问题 When I execute the following command in the Rails project folder: gem install bson_ext I get the this error: #result Building native extensions. This could take a while... ERROR: Error installing bson_ext: ERROR: Failed to build gem native extension. /home/absolute/.rvm/rubies/ruby-1.9.3-p0/bin/ruby extconf.rb checking for asprintf()... yes checking for ruby/st.h... yes checking for ruby/regex.h... yes checking for ruby/encoding.h... yes creating Makefile make compiling bson_buffer.c

mongoid scope with referenced object as criteria

柔情痞子 提交于 2019-12-24 00:52:12
问题 I have following scope for Mongoid model in Rails 3: class Expert include Mongoid::Document referenced_in :category scope :currently_available, lambda { |category, limit| limit ||= 5 { :where => {:category => category, :state => 'available'}, :limit => limit } } category here is an instance of referenced model: class Category include Mongoid::Document references_many :experts, :inverse_of => :category When I call the scope as Expert.currently_available(Category.first, 5) , I got a Criteria

mongoid update elements within array

僤鯓⒐⒋嵵緔 提交于 2019-12-24 00:48:40
问题 I am using mongoid 3.1 with Ruby 1.9.3 and I am trying to update a value within an array. I can successfully perform the following command in mongodb's CLI, but can't seem to find the appropriate solution/translation for mongoid. user.update( { activities: { $elemMatch: { uuid: "1111111-xxxx-xxxx" }}}, { $set: { 'activities.$.submitted': true }}) For context the document looks like: { "_id" : ...., "user_name" : "bob", "activities: [ { uuid: "1111111-xxxx-xxxx", submitted: true, }, { uuid:

Is find_or_create_by thread safe in Mongoid?

孤街浪徒 提交于 2019-12-23 19:12:01
问题 I have a web app that uses Mongoid's find_or_create_by method. poll = Poll.find_or_create_by(fields) Before we go into production, I am trying to run through the failure scenarios, and it occurred to me that multiple users could try to access this resource using this method. Is there any likelihood that it could create multiple instances of the same object? What can I do to prevent that? 回答1: Disclaimer: I'm new to Mongoid and Rails so I could be totally wrong. Looking at modifiable.rb and

Retrieve index of item in array in MongoDB

…衆ロ難τιáo~ 提交于 2019-12-23 19:04:37
问题 similar to -Aggregation: add option to $unwind to emit array index -Get index of an item within mongodb query I have this use case. Tastiest Fruit Rankings: {"date": "Jan 1st", "fruit_ranking": ["Apple", "Orange", "Grape", "Kiwi", "Mango", "Pear"]}, {"date": "Jan 2nd", "fruit_ranking": ["Orange", "Grape", "Kiwi", "Pear", "Apple"]} ..... {"date": "Dec 31st", "fruit_ranking": ["Kiwi", "Apple", "Grape", "Mango", "Pear"]} I'm trying to grab the ranking of "Pear" for each day Jan 1st - Dec 31st

How can i generate mongoid.yml config in Rail 2.3.5?

筅森魡賤 提交于 2019-12-23 17:29:05
问题 As the title says, how can i generate the default mongoid.yml config file on Rail 2.3.5? I try to use the ´rails generate mongoid:config´ command but it just generates a new app. And also, I would like to use has_many in mongoid without embedding the associated model in the same field. I would like them to be in separate fields and associated through a *_id "column". Is that possible? 回答1: You can't. The master branch of MongoID is only Rails 3 compatible. If you want use mongoID with Rails 2

The bug of mongoid returning first document when invoking last?

随声附和 提交于 2019-12-23 16:33:27
问题 I encountered a bug of mongoid, which return first document when I invoking the method last class Post include Mongoid::Document end Post.create Post.create Post.first == Post.last #=> true The versions info: mongoid: "5.0.2" mongodb: v3.2.1 Rails 4.2.5 回答1: That's not a bug in Mongoid, that's a bug in your expectations of the first and last methods. From the fine version 5 manual: #first ⇒ Document Note: Mongoid previously added an _id sort when no sort parameters were provided explicitly by

Mongoid `group()` conditions

谁说胖子不能爱 提交于 2019-12-23 11:45:30
问题 I want to provide a condition for my grouping in Mongoid, but how can I send in multiple values for an attribute in the conditions hash? This is what I want to do: PageViews.collection.group( cond: {page_id: ['4e6912618083ab383e000010', '4e6912618083ab383e000009']}, key: 'timestamp', initial: {count: 0}, reduce: "function(x, y) {y.count += x.count;}" ) So therefore any PageView with the either page_id will be apart of the query, but I can't seem to get the condition hash ( cond ) to work at

With Mongoid, can I “update_all” to push a value onto an array field for multiple entries at once?

狂风中的少年 提交于 2019-12-23 09:41:42
问题 Using Mongoid, is it possible to use "update_all" to push a value onto an array field for all entries matching a certain criteria? Example: class Foo field :username field :bar, :type => Array def update_all_bars array_of_names = ['foo','bar','baz'] Foo.any_in(username: foo).each do |f| f.push(:bar,'my_new_val') end end end I'm wondering if there's a way to update all the users at once (to push the value 'my_new_val' onto the "foo" field for each matching entry) using "update_all" (or