Tag: mongoid

什么时候Mongoid实际上是对MongoDb进行查询

我很难找到明确说明Mongoid实际查询Mongo数据库的文档。 假设我有一个名为Projects的模型,它引用了另一个名为Website的模型,如下所示 class Project include Mongoid::Document … has_one :website … end class Website include Mongoid::Document … belongs_to :project … end 如果使用我的Project模型,我有许多访问Website对象属性的方法,我的问题是,Mongodb的实际查询出现在哪一行: class Project … def website_url @website ||= self.website #Does the query occur here? website.url # Or does is occur here? end 我可以预见到,在请求对象的实际属性或属性非常有益之前,推迟查询数据库的情况。 但是,我不知道如何测试这个以确定自己的答案。 谢谢

如何对mongodb / mongoid脚本进行基准测试,以比较两种不同的查询技术

您是否有关于如何在两种不同的mongoid / mongodb查询实现上测试性能的建议? 要比较的实现与先前的“问答”相关,但为了简洁起见,我将再次在此处报告: 第一个代码: Competitor = Struct.new(:html_url, :description, :user) competitors = [] User.all.map do |user| user.watchlists.all.map do |wl| if wl.tags_array == [“ruby”, “web”, “framework”] competitors << Competitor.new(wl.html_url, wl.description, wl.user.nickname) end end end 与第二代码: Competitor = Struct.new(:html_url, :description, :user) competitors = [] User.where(‘watchlists.tags_array’ => %w[ruby web framework]).only(:nickname, :watchlists).each do |u| u.watchlists.where(:tags_array => %w[ruby web framework]).each […]

Mongoid – 通过引用文档查询

我有一个名为Ad的模型,如下所示: class Ad include Mongoid::Document referenced_in :category end 和类别模型: class Category include Mongoid::Document referenced_in :domain references_many :ads end 如何按域选择广告? 我曾尝试使用Ad.where(‘category.domain_id’ => domain.id)但这不起作用。

在MongoDB中存储数据的有效方法:嵌入式文档与单个文档

我存储用户活动数据:当用户访问当前文章,主题或个人消息时,向他显示他在离线时添加了多少新评论和消息。 class SiteActivity include Mongoid::Document include Mongoid::Timestamps belongs_to :user belons_to :activity, polymorphic: true end 在这种情况下,我为每个文档存储一条记录。 另一种选择是使用嵌入式文档,因此所有用户活动都将存储在一个文档中: class SiteActivity include Mongoid::Document belongs_to :user embeds_many :user_activities validates :user_id, uniqueness: true end class UserActivity include Mongoid::Document include Mongoid::Timestamps embedded_in :site_activity belongs_to :activity, polymorphic: true end 所以现在我不需要搜索所有SiteActivities(许多记录)但我可以为current_user获取一个user_activity并通过它嵌入文档找到我需要的活动。 哪种方式更有效地存储和搜索数据? 我的普通用例是: 我有一个用户和一个post,所以我使用这些数据获取site_activity,以查看此用户上次访问post的日期。 我的第一个选择: activity = SiteActivity.where(user_id: current_user.id, activity_id: post.id, activity_type: post.class) […]

Mongoid – 同一外域的两个场反转

我正试图让以下Mongoid关系起作用,但每个团队的游戏领域都是一个空数组。 这不是一个有效的关系模型吗? 我是否需要拆分游戏,即home_games和away_games? class Team include Mongoid::Document has_many :games, :autosave => true end class Game include Mongoid::Document belongs_to :home_team, :class_name => “Team”, :inverse_of => :games belongs_to :away_team, :class_name => “Team”, :inverse_of => :games end

mongoid中嵌入关系的真实多态性

使用Mongoid3,我试图将多态性添加到我的嵌入式关系中。 我有一个类Item ,必须嵌入包含我的信息的对象。 这笔交易是: – 我的对象的类型可以是其中之一: Calendar , Sticker , Picture ; – 无论我的对象的类型如何,我想通过一个独特的“密钥”访问它: 详细信息 例如: pry> my_item1.detail => “ pry> my_item2.detail => “ pry> 首先,我尝试使用像这里描述的关键字as和polymorphic : https : //github.com/mongoid/mongoid/issues/902 例如: class Item include Mongoid::Document embeds_one :detail, as: :item_slot end class Picture include Mongoid::Document embedded_in :item_slot, polymorphic: true end class Calendar include Mongoid::Document embedded_in :item_slot, […]

Ruby / rails:mongoid with mongo(gem); 一场激烈的冲突? 如何处理不同的版本?

如何处理gem版错配? Mongoid 4.0.0(最新版)使用moped(2.0.0),需要bson 2.3 Mongo 1.10.2(最新)使用bson 1.10.2 我不能一起使用mongoid 4.0.0和mongo 1.10.2,但我可以在过去,当mongoid使用bson 1.x. 有谁知道如何继续使用mongo和mongoid? -daniel 链接: https://rubygems.org/gems/mongoid https://rubygems.org/gems/moped https://rubygems.org/gems/mongo

将字符串ID与BSON :: ObjectId进行比较

我有一个由BSON::ObjectId类型组成的数组,我希望它与一些ID作为字符串进行比较。 if my_array_of_BSON_ObjectIds.include?(@my_id_as_a_string) # delete the item from the array else # add the item to the array as a BSON::ObjectId end 由于类型不同,这不起作用,我可以将我的字符串转换为BSON::ObjectId吗? 如果是这样,怎么样?

按天分组Mongoid对象

在控制台中玩了很多次之后,我想出了这种方法,可以在它们发生的那一天将类似activerecord的(Mongoid)对象分组。 我不确定这是实现这一目标的最佳方法,但它确实有效。 有没有人有更好的建议,或者这是一个好方法吗? #events is an array of activerecord-like objects that include a time attribute events.map{ |event| # convert events array into an array of hashes with the day of the month and the event { :number => event.time.day, :event => event } }.reduce({}){ |memo,day| # convert this into a hash with arrays of events […]

rails mongoid明确的标准

Mongoid :: Paranoia为生成标准的模型添加默认范围 #{“$exists”=>false}}, options: {}, class: Line, embedded: false> 我可以找到生成的Model.deleted的已删除文档, #{“$exists”=>true}}, options: {}, class: Line, embedded: false> 我如何覆盖这个,以便我可以搜索已删除和未删除的文档。 PS Model.unscoped不起作用