Tag: mongodb

如何对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) […]

Ruby Mongodb驱动程序$ gt在日期条件下充当$ gte,为什么?

我在ruby mongodb驱动程序中使用$gt和$gte条件有一个奇怪的情况。 所以这是代码: timeline = timeline_db.find({date: {“$gt” => s_time}, username: { “$in” => followers_array } }, sort:[“date”, Mongo::DESCENDING], limit: 10) 问题是这个查询返回的项目恰好是我正在请求的时间,如果我写了$gte ,这将是有效的。 $gte确实返回与$gt完全相同的结果。 为什么会这样?

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吗? 如果是这样,怎么样?

从mongoDB中删除文档

这可能是一个非常愚蠢的问题,但我是MongoDB的新手,所以请耐心等待。 我创建了一个独立的ruby类: require ‘rubygems’ require ‘mongo’ require ‘bson’ require ‘mongo_mapper’ MongoMapper.database = “testing” class Twit include MongoMapper::Document key :id, Integer, :unique => true key :screen_name, String, :unique => true … 然后我用irb执行以下操作 >> twit = Twit.all.first => # >> twit.destroy => true >> Twit.all => [#] 那么如何销毁MongoDB中的文档呢? 我究竟做错了什么?

mongo db中的tailable游标超时

我正在尝试在ruby中创建一个oplog观察器。 到目前为止,我已经在下面提出了一个小脚本。 require ‘rubygems’ require ‘mongo’ db = Mongo::Connection.new(“localhost”, 5151).db(“local”) coll = db.collection(‘oplog.$main’) loop do cursor = Mongo::Cursor.new(coll, :tailable => true) while not cursor.closed? if doc = cursor.next_document puts doc else sleep 1 end end end 这个问题是,在5或6秒之后它吐出大量数据时会超时并且我得到一个错误 C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/mongo-1.4.0/lib/../lib/mongo/connection.rb :807:in `check_response_flags’: Query response returned CURSOR_NOT_FOUND. Either an invalid c ursor was specified, or the cursor may […]

未定义的方法’dragonfly_accessor’

我是dragonfly的新手,并尝试在主要文档页面上查看rails的设置说明。 不使用Active Record。 我的步骤: 1)添加到Gemfile gem ‘dragonfly’, “~>1.0.3” 2) bundle install 3) rails g dragonfly 创建初始化器/ dragonfly.rb 4)模型 class Post include Mongoid::Document dragonfly_accessor :image field :title, type: String field :body, type: String end 5)控制器 params.require(:post).permit(:title, :body, :image) 刚刚运行mongod和rails s,我得到了未定义方法’dragonfly_accessor’的错误 我缺少什么想法?

Mongoid和ActiveRecord关系:未定义的方法`quoted_table_name’

class Contest NoMethodError: undefined method `quoted_table_name’ for ClaimTemplate:Class 好的,我们将quoted_table_name添加到ClaimTemplate : def self.quoted_table_name “claim_templates” end # console Contest.new.claim_template #=> nil # Cool! # But: Contest.last.claim_template #=> TypeError: can’t convert Symbol into String 那么如何配置我的模型以便彼此正常工作 PS: 现在我有这种结构,工作正常,但我希望获得关系( Assosiations )的好处。 class Contest self.id).first end # Mongoid going to be crazy without this hack def self.using_object_ids? false end end