mongodb-ruby

Ruby Mongo Driver: How To Look For Date Intervals?

孤街浪徒 提交于 2020-03-05 03:09:52
问题 Given a init date and today, what's the query to search for all "names" between that date? Thank you 回答1: MongoMapper You should be able to use MongoMapper's query operators. Suppose your have a "User" model with a "created_on" date, you could use this to get the names. (I believe MongoDB uses UTC Times to store all date/time objects): initial_date = Time.utc(2011, 5, 1) # 2011-05-01 00:00:00 UTC @users = User.where(:created_on => {:$gte => initial_date, :$lte => Time.now.utc}) @users.each do

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

Ruby group hashes by value of key

大憨熊 提交于 2019-12-05 10:47:59
问题 I have an array, which is output by a map/reduce method performed by MongoDB, it looks something like this: [{"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>299.0}, {"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>244.0}, {"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>1.0, "count"=>204.0}, {"minute"=>45.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0,

Ruby group hashes by value of key

本秂侑毒 提交于 2019-12-03 22:59:47
I have an array, which is output by a map/reduce method performed by MongoDB, it looks something like this: [{"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>299.0}, {"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>244.0}, {"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>1.0, "count"=>204.0}, {"minute"=>45.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>510.0}, {"minute"=>45.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0,

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

只谈情不闲聊 提交于 2019-12-03 11:52:15
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. Vamshidhar Behara Got it! Mongoid.default_session.options[:database] The new way to get this is Mongoid::Config.clients["default"]["database"] You can also just have a look at Mongoid::Config.clients to see what else is available. If you want the overrided database you

MongoDB - too much data for sort() with no index error

好久不见. 提交于 2019-11-29 05:51:36
问题 I am using MongoDB 1.6.3, to store a big collection (300k+ records). I added a composite index. db['collection_name'].getIndexes() [ { "name" : "_id_", "ns" : "db_name.event_logs", "key" : { "_id" : 1 } }, { "key" : { "updated_at.t" : -1, "community_id" : 1 }, "ns" : "db_name.event_logs", "background" : true, "name" : "updated_at.t_-1_community_id_1" } ] However, when I try to run this code: db['collection_name'] .find({:community_id => 1}) .sort(['updated_at.t', -1]) .skip(@skip) .limit(

Getting the highest value of a column in MongoDB

旧街凉风 提交于 2019-11-27 19:02:51
I've been for some help on getting the highest value on a column for a mongo document. I can sort it and get the top/bottom, but I'm pretty sure there is a better way to do it. I tried the following (and different combinations): transactions.find("id" => x).max({"sellprice" => 0}) But it keeps throwing errors. What's a good way to do it besides sorting and getting the top/bottom? Thank you! max() does not work the way you would expect it to in SQL for Mongo. This is perhaps going to change in future versions but as of now, max,min are to be used with indexed keys primarily internally for

Getting the highest value of a column in MongoDB

▼魔方 西西 提交于 2019-11-26 22:45:42
问题 I've been for some help on getting the highest value on a column for a mongo document. I can sort it and get the top/bottom, but I'm pretty sure there is a better way to do it. I tried the following (and different combinations): transactions.find("id" => x).max({"sellprice" => 0}) But it keeps throwing errors. What's a good way to do it besides sorting and getting the top/bottom? Thank you! 回答1: max() does not work the way you would expect it to in SQL for Mongo. This is perhaps going to