mongoid

pluck vs distinct in mongoid db. which is faster?

半腔热情 提交于 2019-12-10 23:16:33
问题 It seems that mongodb has two equivalent methods: #pluck and #distinct which both return only given fields from a collection. so both User.pluck(:name) User.distinct(:name) would return array of all names from User collection in db > ['john', 'maria', 'tony', 'filip'] I don't mind duplicates. Which method is faster? 回答1: Let's run a benchmark! require 'benchmark' 1_200.times { FactoryGirl.create(:user) } Benchmark.bmbm(7) do |bm| bm.report('pluck') do User.pluck(:email) end bm.report('pluck

Mongoid query by value or default value

久未见 提交于 2019-12-10 23:09:06
问题 I've just updated one of my models with a boolean field. I've set a default value for the field to true. How can I query for this field in such a way that I get all documents with this field set to true or doesn't have this field (default value). 回答1: To find documents that don't have a particular key, you want to use $exists: $exists Syntax : { field: { $exists: <boolean> } } $exists selects the documents that contain the field if <boolean> is true. If <boolean> is false, the query only

Mongoid 3.1.0 CounterCache doesn't work

青春壹個敷衍的年華 提交于 2019-12-10 22:29:56
问题 I'm trying to use Mongoid CounterCache but it doesn't work. I tried to just use belongs_to :user, counter_cache: true But it returns Problem: Invalid option :counter_cache provided to relation :user. Summary: Mongoid checks the options that are passed to the relation macros to ensure that no ill side effects occur by letting something slip by. Resolution: Valid options are: autobuild, autosave, dependent, foreign_key, index, polymorphic, touch, class_name, extend, inverse_class_name, inverse

Make mongoid session read only

为君一笑 提交于 2019-12-10 21:18:17
问题 I have different sessions in my mongoid.yml , where one session provides data from a static mongo database. I was wondering if it is possible, to "load" a session in read only mode, so that no changes with save , create , destroy or destroy_all can be made. My mongoid.yml looks like this: production: sessions: default: database: my_app_production hosts: - localhost:27017 options: read: primary static_content: database: static_content_db hosts: - localhost:27017 options: read: primary options:

Mongoid 3 - Check for uniqueness of composite key

感情迁移 提交于 2019-12-10 21:16:24
问题 I switched to Mongoid 3 which makes few things different :) Currently I try to check if a composite field is unique: class Host include Mongoid::Document field :ip, :type => String field :port, :type => Integer field :username, :type => String field :password, :type => String validates_presence_of :ip validates_presence_of :port end How to get a validates_uniqueness_of therein which should check if ip and port are unique as composite field? AFAIK there was a way in Mongoid 2 to create a new

mongoid polymorphic association error

我怕爱的太早我们不能终老 提交于 2019-12-10 20:37:25
问题 I'm having some problems using mongoid-3.0.6 with polymorphic fields. Using rails 3.2.8 and ruby 1.9.3 Using a normal polymorphic relation: class Shirt include Mongoid::Document field :name, localize: true belongs_to :itemizable, polymorphic: true end class Item include Mongoid::Document field :price, type: Float field :quantity, type: Integer, :default => 1 has_one :product, as: :itemizable accepts_nested_attributes_for :product end The same association is available through the metadata: >>

Mongoid - limit on distinct query

老子叫甜甜 提交于 2019-12-10 19:07:04
问题 I am trying to put a limit on the number of distinct results returned from a mongoid query. Place.where({:tags.in => ["food"]}).distinct(:name).limit(2) But this throws up the following error: NoMethodError: undefined method 'limit' for ["p1", "p2", "p3", "p4"]:Array How do I put a limit in the mongoid query instead of fetching the entire result set and then selecting the limited number of items from the array. Thanks 回答1: distinct in MongoDB is a command and commands don't return cursors.

Mongoid 4 / MongoDB 2.4+ “Freezing” Issue

允我心安 提交于 2019-12-10 18:02:20
问题 I've had a strange issue since with maintaining a connection to mongodb using Mongoid. I thought this was originally due to upgrading to Rails 4.0+ (which required Mongoid/Moped to be updated), but I've also had this issue with other Rack-based apps (Sinatra and Grape to be specific). We updated our mongodb instance from 2.4 to 2.6 after the stable was released, but this issue was present on 2.4 as well. I wrote this up in an issue on Github (https://github.com/mongoid/moped/issues/274) but I

Validating embedded document in Mongoid based on embedded attribute

橙三吉。 提交于 2019-12-10 17:47:08
问题 I've a class Subscriber which has embeds_many Subscriptions. Subscription has an attribute status. I want to add a validation on status such that only one Subscription can have status 'active' per subscriber. The subscriber can have multiple subscription with status 'purchased' or 'expired' . 回答1: This should do it: class Subscriber include Mongoid::Document embeds_many :subscriptions validate :active_subscriptions def active_subscriptions self.errors.add(:base, 'too many active subscriptions

Mongoid custom setters / getters and super

早过忘川 提交于 2019-12-10 17:45:27
问题 I'm trying to modify a setter on an attribute Mongoid model, but unlike ActiveRecord, I cannot call super to have Mongoid actually set the attribute, as the model is using include Mongoid::Document rather than a subclass of ActiveRecord::Base . I want to be able to do something like this. class User include Mongoid::Document embeds_one :email_account def email_account=(_email_account) ret = super puts "email account updated!" do_something ret end end except, as its not a subclass, yields