mongoid3

How to programmatically get the current database Mongoid is writing to?

不羁的心 提交于 2020-01-01 04:17:07
问题 I am talking to multiple databases using Mongoid.override_database("database_name") using Mongoid with rails. How do I find the current database programmatically? Mongoid docs on sessions: http://mongoid.org/en/moped/docs/driver.html define methods to override database but do not define a way to get the current database in use. 回答1: Got it! Mongoid.default_session.options[:database] 回答2: The new way to get this is Mongoid::Config.clients["default"]["database"] You can also just have a look at

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 to get print output for debugging map/reduce in Mongoid?

好久不见. 提交于 2019-12-19 06:59:06
问题 I'm writing a map/reduce operation with Mongoid 3.0. I'm trying to use the print statement to debug the JS functions. This is a troubleshooting suggestion from the MongoDB docs, e.g.: reduce = %Q{ function(user_id, timestamps) { var max = 0; timestamps.forEach(function(t) { var diff = t.started_at - t.attempted_at; if (diff > max) { max = diff; } }); print(user_id + ', ' + max); return max; }; } MyCollection.all.map_reduce(map, reduce).to_a Unfortunately the output from the print statement

undefined method `paginate' for Array on Rails 3 with mongoid

耗尽温柔 提交于 2019-12-13 18:25:12
问题 Trying to use will_paginate gem. My Gem file has: gem 'will_paginate', '~> 3.0.0' My orders_controllers.rb: def index @orders = Order.all.paginate(:page => params[:page], :per_page => 20) respond_to do |format| format.html # index.html.erb format.json { render json: @orders } end end On my index.html.erb I've put: <%= will_paginate @orders %> Error: NoMethodError in OrdersController#index undefined method `paginate' for #<Array:0x007f79a1be62f8> Rails.root: /home/askar/Dropbox/rails_studio

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

Heroku not using the specified Ruby 1.9.3

那年仲夏 提交于 2019-12-11 03:48:18
问题 Upgrading to Mongoid 3 and Ruby 1.9.3. Trying to use Ruby 1.9.3 on Heroku Cedar Stack. I have added the below to the top of my Gemfile: source 'http://rubygems.org' ruby "1.9.3" Pushing to Heroku, it seemed to be working -----> Heroku receiving push -----> Ruby/Rails app detected -----> Using Ruby version: ruby-1.9.3 -----> Installing dependencies using Bundler version 1.2.0.rc.2 However my app is breaking as it requires Ruby 1.9.3. The error is coming as: TypeError: can't convert Array into

Advice on migrating from MongoMapper to Mongoid?

瘦欲@ 提交于 2019-12-05 18:56:35
问题 It seems like Mongoid is now the superior ORM for Mongo based on performance and development activity. Unfortunately, we're on MongoMapper and need to migrate. Are there any concerns or stumbling blocks we should be aware of? We have found a few outdated articles on Google and tried posting on the Mongoid Google Groups (though we were prohibited), but would love thoughts from SO members who have done this in the past. We're on Rails 3.2.12. Thanks! 回答1: Both of them are great MongoDB

Rails Engine + Mongoid: No configuration could be found for a session named 'default'

坚强是说给别人听的谎言 提交于 2019-12-04 09:36:11
问题 I've created a Rails Mountable App and added 'mongoid' and 'rspec' gem's. If I try to run my specs now I get the following error: Mongoid::Errors::NoSessionConfig: Problem: No configuration could be found for a session named 'default'. Summary: When attempting to create the new session, Mongoid could not find a session configuration for the name: 'default'. This is necessary in order to know the host, port, and options needed to connect. Resolution: Double check your mongoid.yml to make sure