Tag: mongodb

在mongodb ruby​​本地驱动程序中的Mapreduce

我正在使用mongodb的原生ruby驱动程序。我想用它执行map reducefunction,我试着按照这个链接在这里给出 。 我undefined method last for nil:NilClass得到了错误状态的undefined method last for nil:NilClass 我尝试的代码是 def self.mapreduce begin # remove old decoments DBEngine.remove_alldocuments(COLLECTION_MAPREDUCE) # sample collection insertion to perform map reduce function 3.times do |ctr| document = { “cust_id” => “A12”, “amount” => 200, “status” => “S” } result = DBEngine.insert(COLLECTION_MAPREDUCE,document) puts result end 2.times do |ctr| […]

使用mongoid在mongodb中使用mongodb嵌入消息回复

我认为最好的做法是将回复嵌入到该消息中的特定消息中,并且我正在尝试使用mongoid来实现它。 这就是我所拥有的 class Message include Mongoid::Document include Mongoid::Timestamps include Mongoid::Paranoia field :subject field :body field :sender_deleted, :type => Boolean, :default => false field :recipient_deleted, :type => Boolean, :default => false field :read_at, :type => DateTime referenced_in :sender, :class_name => “User”, :inverse_of => :sender, :foreign_key => ‘sender_id’ referenced_in :recipient, :class_name => “User”, :inverse_of => :recipient, :foreign_key […]

有关从MongoMapper迁移到Mongoid的建议?

看起来Mongoid现在是基于性能和开发活动的Mongo的优秀ORM。 不幸的是,我们使用MongoMapper并需要迁移。 我们应该注意哪些问题或绊脚石? 我们在Google上发现了一些过时的文章,并尝试在Mongoid Google网上论坛上发帖(虽然我们被禁止),但是会喜欢过去做过这些的SO成员的想法。 我们在Rails 3.2.12上。 谢谢!

蒙古族中的暧昧关系

我正在尝试在Post模型中使用viewer_ids来保存user_ids,并将User模型中的Viewed_ids保存到已查看的post_ids中。 使用Rspec进行测试添加/删除并访问User的关系时,它的效果非常好。 但是当我使用RABL来查看post-while用户数据时,它会被混淆并给我一个模糊的关系。 #Post class belongs_to :user has_and_belongs_to_many :viewers, class_name: ‘User’, inverse_of: :viewed #User class has_many :users has_and_belongs_to_many :viewed, class_name: ‘Post’, inverse_of: :viewers 邮件#show中的Mongoid :: Errors :: AmbiguousRelationship Problem: Ambiguous relations :posts, :viewed defined on User. Summary: When Mongoid attempts to set an inverse document of a relation in memory, it needs to know which […]

将rails mongodb应用程序部署到heroku时“预编译资产失败”

我正在尝试使用mongodb部署rails应用程序到heroku,但不断收到错误: NoMethodError: undefined method `active_record’ for # … Precompiling assets failed. 在问之前我已经尝试过所有内容,例如以下解决方案 – ( Heroku预编译资产失败 , Ruby on Rails预编译资产失败 )但我仍然无法使其工作 1.当我创建应用程序时,我使用-O选项跳过活动记录: rails new app -T -O 2.我删除了sqlite3。 这是Gemfile: source ‘https://rubygems.org’ gem ‘rails’, ‘4.1.8’ gem ‘sass-rails’, ‘~> 4.0.3’ gem ‘uglifier’, ‘>= 1.3.0’ gem ‘coffee-rails’, ‘~> 4.0.0’ gem ‘jquery-rails’ gem ‘turbolinks’ gem ‘jbuilder’, ‘~> 2.0’ gem ‘sdoc’, […]

Mongoid聚合$匹配Date对象不起作用?

我有一个具有这种结构的集合中的用户文档: { “_id” : ObjectId( “4fb54ef46d93b33b21003951” ), “activities” : [ { “id” : ObjectId( “4fd66f9001e7fe9f03000065” ), “type” : “checkin”, “date_time_created” : Date( 1339453328000 )}, { “date_time_created” : Date( 1337351732000 ), “date_time_updated” : Date( 1337351952635 ), “id” : ObjectId( “4fb65e346d93b3fe77000000” )} ] } 我可以根据日期轻松查询这些文档: User.where( :activities => { ‘$elemMatch’ => { :date_time_created => { ‘$gte’ => […]

Mongoid排序模型基于数组大小,在其他Model(has_one)关系中

我有Postactivity模型,它有post_id作为Post Model的外键(has_one关系),这个Postactivity模型有Postactivity的数组。 我如何按likes对Post模型进行排序? class Post has_one :postactivity, foreign_key: :post_activity_id, class_name:”PostActivity” end class PostActivity field :likes, type: Array belongs_to :post, foreign_key: :post_id, class_name: “Post” end

如何从Ruby代码连接到mongodb?

如何从Ruby代码连接到mongodb?

基于具有has_many关联的子集合计数的查询始终显示无结果

我正在尝试查询子集合中的文档数量。 这是我的背景: class Pro include Mongoid::Document has_many :recommendations scope :lost, -> { where(:recommendation_ids => []) } scope :normal, -> { where(:recommendation_ids.ne => []) } end 和儿童collections: class Recommendation include Mongoid::Document belongs_to :pro end 所以,现在我执行: >> Pro.lost.count 0 >> Pro.all.select{|p| p.recommendations.count == 0}.count 1 我究竟做错了什么? 我也试过Pro.with_size(recommendation_ids: 0)和其他一些变化,但没什么新的。 任何想法都将受到高度赞赏,提前感谢。 Moped 2.0.1,Mongoid 4.0.0,Rails 4.0.6

Mongoid按日期分组

这是我的文件: { event_type: ‘click’, created_at: ‘2015-02-15 10:00:00’, user_id: 100 } 我需要每天使用event_type = click对唯一身份用户求和。 得到类似的东西: { count: 320, date: ‘2015-02-15’ } { count: 451, date: ‘2015-02-16’ } (注意日期格式) 我尝试了很多我在SO上找到的不同的东西,这个对我来说几乎有用 : 用Mongoid按日期分组的最佳方法我只是想不出如何修改它来完全按照我的需要做。 这是几乎做我想用Mongoid实现的原始查询: db.events.aggregate([ {$match: {event_type: ‘click’, created_at: { $gte: ISODate(‘2015-01-01T00:00:00.000Z’), $lt: ISODate(‘2016-01-01T00:00:00.000Z’) }}}, {$group: { _id: {user_id: ‘$user_id’, account_id: ‘$account_id’, created_at: { day: { $dayOfMonth: ‘$created_at’ […]