mongoid

MongoDB query an array for an exact element match, but may be out of order

时光总嘲笑我的痴心妄想 提交于 2019-12-10 16:12:18
问题 I have a document in MongoDB that looks like this: { users: ["2", "3", "4"] } I'm try to query this document by matching the users array. db.things.find( { users: { $all: [ "2", "3", "4" ] } } ) That query works, but would also return this document: { users: ["2", "3", "4", "5"] } Last requirement is to be able to query the users array with the elements out of order, say [ "3", "4", "2" ] in the query, and it be able to return my first document listed. Any help would be greatly appreciated.

Mongoid embeds_many: push document without save in order to preserve dirty state

橙三吉。 提交于 2019-12-10 15:03:15
问题 In Mongoid, pushing a document into an embeds_many relation automatically persists the document to the database. Normally, this is fine, but I run into problems when I need to track changes to the embedded document. Say you have two models: class List include Mongoid::Document embeds_many :items field :title end class Item include Mongoid::Document embedded_in :list field :name end This happens to the .changes attribute: list = List.new(title: 'List title') list.save #list is now persisted

Unable to get a covered query to use index only in mongodb

╄→尐↘猪︶ㄣ 提交于 2019-12-10 13:47:23
问题 I am trying to use a covering index to implement stemming text search on my app which uses mongodb. I've got the following index set: ensureIndex({st: 1, n: 1, _id: 1}); But when I run explain() on my query, I can never get the indexOnly to read true, no matter what I do. db.merchants.find({st: "Blue"}, {n:1,_id:1}).explain() { "cursor" : "BtreeCursor st_1_n_1__id_1", "nscanned" : 8, "nscannedObjects" : 8, "n" : 8, "millis" : 0, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : true,

embedded document vs hash datatype in mongoid

北战南征 提交于 2019-12-10 12:53:42
问题 I can't find any blog post or documentation talking about this. They both, embedded document and hash data type, are quite similar. What the benefit or limitation of each over the other? Consider my schema design: class HistoryTracker include ::Mongoid::Document include ::Mongoid::Timestamps field :modifier, type: Hash, default: {} field :original, type: Hash, default: {} field :modified, type: Hash, default: {} field :changeset, type: Hash, default: {} end Should I create several embedded

How to enable SSL/TLS in Mongoid 3 client?

爷,独闯天下 提交于 2019-12-10 11:49:41
问题 How do you enable SSL/TLS in the Mongoid 3 client? I've tried: options: -ssl: true But I get "undefined method `each_pair' for [{"ssl"=>true}]:Array (NoMethodError)": /home/user1/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/bundler/gems/mongoid-026e32109178/lib/mongoid/config.rb:203:in `options=': undefined method `each_pair' for [{"ssl"=>true}]:Array (NoMethodError) from /home/jwiley/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/bundler/gems/mongoid-026e32109178/lib/mongoid/config.rb:129

Limit number of objects in has_many association with mongoid

眉间皱痕 提交于 2019-12-10 10:25:33
问题 I want to limit the number of associated objects in a has_many association between a post and pictures : In active record i can do something like class post < < ActiveRecord::Base has_many :pictures, :limit => 2 end But mongoid raise an exception with limit: Invalid option :limit provided to relation :pictures. Valid options are: as, autosave, dependent, foreign_key, order, class_name, extend, inverse_class_name, inverse_of, name, relation, validate. (Mongoid::Errors::InvalidOptions) Is there

Mongoid::Errors::MixedRelations

倾然丶 夕夏残阳落幕 提交于 2019-12-10 10:19:53
问题 I've a User model embedding "one to many" Watchlists like the following : class User include Mongoid::Document field :uid field :name field :user_hash embeds_many :watchlists end class Watchlist include Mongoid::Document field :html_url field :description #field :name field :fork_, :type => Boolean field :forks, :type => Integer field :watchers, :type => Integer field :created_at, :type => DateTime field :pushed_at, :type => DateTime field :avatar_url embedded_in :user has_and_belongs_to_many

How to change a document's _type in Mongoid?

喜欢而已 提交于 2019-12-10 04:19:28
问题 I have the following models inside a Rails application: class User include Mongoid::Document ... end class Admin < User ... end I get a user: u = User.find(some_key) And try to change the _type: u._type # => "User" u._type = "Admin" u.save u._type # => "Admin" But if I reload the object it's still a user: u.reload u._type = "User" What is the correct way to change this? 回答1: you could also use Model#update_attribute to stay with mongoid: user.update_attribute(:_type, "Admin") 回答2: Ended up

Rails Mongoid model query result returns wrong size/length/count info even when using limit

北慕城南 提交于 2019-12-10 03:30:54
问题 When querying on a certain model in my rails application, it returns the correct results, excerpt the size , length or count information, even using the limit criteria. recipes = Recipe .where(:bitly_url => /some.url/) .order_by(:date => :asc) .skip(10) .limit(100) recipes.size # => 57179 recipes.count # => 57179 recipes.length # => 57179 I can't understand why this is happening, it keeps showing the total count of the recipes collection, and the correct value should be 100 since I used limit

Mongoid: And - Or query

你说的曾经没有我的故事 提交于 2019-12-10 01:16:13
问题 I have this query: User.or( { name: 'John' }, { name: 'Sara' } ).or( { age: 17 }, { age: 18 } ) ) It returns the next Criteria: #<Mongoid::Criteria selector: {"enabled"=>true, "$or"=>[{"name"=>"John"}, {"name"=>"Anoun"}, {"age"=>17}. {"age"=>18}]} options: {} class: User embedded: false> But I want to do 'and' betweend two 'or' that return something like this: #<Mongoid::Criteria selector: {"enabled"=>true, "$and"=>[ {"$or"=>[{"name"=>"John"}, {"name"=>"Anoun"}]}, {"$or"=>[{"age"=>17}, {"age"