mongoid

Is it bad to change _id type in MongoDB to integer?

穿精又带淫゛_ 提交于 2019-12-17 10:57:31
问题 MongoDB uses ObjectId type for _id. Will it be bad if I make _id an incrementing integer? (With this gem, if you're interested) 回答1: No it isn't bad at all and in fact the built in ObjectId is quite sizeable within the index so if you believe you have something better then you are more than welcome to change the default value of the _id field to whatever. But, and this is a big but , there are some considerations when deciding to move away from the default formulated ObjectId , especially

Batch insert/update using Mongoid?

六眼飞鱼酱① 提交于 2019-12-17 07:07:18
问题 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? 回答1: You can insert a batch array of hashes using the ruby mongo driver's insert method. From any Mongoid class, you can call collection to access it. batch = [{:name => "mongodb"}, {:name => "mongoid"}] Article.collection.insert(batch) 回答2: If you want to batch insert Mongoid documents (models) instead of hashes, call your model's as_document method before placing

MongoDB: count the number of items in an array

做~自己de王妃 提交于 2019-12-16 19:55:10
问题 I have a collection where every document in the collection has an array named foo that contains a set of embedded documents. Is there currently a trivial way in the MongoDB shell to count how many instances are within foo ? something like: db.mycollection.foos.count() or db.mycollection.foos.size() ? Each document in the array needs to have a unique foo_id and I want to do a quick count to make sure that the right amount of elements are inside of an array for a random document in the

Duplicate Records created from Association

与世无争的帅哥 提交于 2019-12-14 04:06:09
问题 I am using Mongoid, Rails and Fabrications and at a total loss with how this is happening. Any thoughts very appreciated, but I know this pretty complicated. I just want to fabricate a user and have only four joined groups, but I keep getting eight loaded. Here is the relevant section of my code @user1 = Fabricate.build(:registered) @user1.joined_groups << [common_group, cali_group, ca46, Fabricate(:polco_group, {:name => "Gang of 13", :type => :custom})] When I run @user1.joined_groups.size

Mongoid ignores collection.insert if at least one duplicate exists

断了今生、忘了曾经 提交于 2019-12-13 21:40:34
问题 I have a unique index setup, with drop_dup: true index({ key1: 1, key2: 1 }, { unique: true, drop_dups: true }) When inserting multiple records, I would like the non-duplicates to succeed, similar to MySQL's INSERT IGNORE INTO table ... .. or even INSERT INTO table ... ON DUPLICATE KEY UPDATE So if I have a record: Model.collection.insert({'key1'=>'not-unique', 'key2'=>'boo'}) It seems that the following call doesn't do anything. Model.collection.insert( {'key1'=>'not-unique', 'key2'=>'boo'},

Executing mongodb scripts via mongoid Rails

亡梦爱人 提交于 2019-12-13 19:34:21
问题 I have a mongo db script in a js file: query.js //conn = new Mongo(); //db = conn.getDB("dbName"); functionFoo = function (arg){ //----process arg } also I have an array of args known as args_array , (that I fetch from database using mongoid) for which I want to do something like this: args_array.each do |arg| //some how call functionFoo(arg) from the query.js file end is this possible in rails? I am able to execute the file from terminal but I want to wrap it in my application so that I can

mongoid combination of any_of and between

纵饮孤独 提交于 2019-12-13 18:00:27
问题 I need to achieve something like: queryable.any_of( between(:average_nightly_min_price, [10,100]), between(:average_nightly_max_price, [110,1100]) ) is it possible using the combination of any_of and between . Or any other alternatives? 回答1: queryable.any_of( queryable.between(:average_nightly_min_price, [10,100]).selector, queryable.between(:average_nightly_max_price, [110,1100]).selector ) It does not work if the queryable is embeded. For ruby versions prior 2.0: queryable.any_of( queryable

Rails simulating Constraints and sub-Constraints

我与影子孤独终老i 提交于 2019-12-13 17:55:33
问题 I am building a scheduling platform with tasks for a current project Let's say there are some Global Constraints (money, people, etc.) for the project, and that each tasks also has individual Constraints. The big deal is that I have to match each Global Constraint to every Constraint of the tasks of a same project Now the real big deal, is that in addition to having a nature (financial -> Float, people -> Integer) the constraints can also have a type (ex : costs of wages, costs of raw

Mongoid Query DB by Virtual Attribute

三世轮回 提交于 2019-12-13 17:49:42
问题 BACKGROUND: I have Posts and Users, both of which HABTM Communities. In my Post model, I have a method for calculating a post's relevance to a given user by comparing how many communities they have in common as such: def relevance(user) (self.communities & user.communities).length end OBJECTIVE: To query Posts either by a given relevance i.e. Post.where(:relevance => 3) or to query all posts and sort them by relevance i.e. Post.all.desc(:relevance) I know that the user variable is needed

mongodb mongoid-rails unable to increment counters in a hash in embedded document

风流意气都作罢 提交于 2019-12-13 16:45:34
问题 I am using Mongoid 4.0.2 , Rails 4.2.1 amd Mongodb 2.4.8. I have daily_events as an embedded document in person collection as shown below. class Person include Mongoid::Document field :email, type: String field :month, type: Date field :monthly_total, type: Hash, default: {:email_open => 0, :email_click => 0, :page_view => 0} embeds_many :daily_events end class DailyEvent include Mongoid::Document field :name, type: String field :day_1, type: Hash, default: -> {:email_open => 0, :email_click