Tag: mongoid

使用Rails和Mongoid的动态属性

我正在通过带有Rails(Rails 3 beta 3)的Mongoid Ruby gem学习MongoDB,我正试图想出一种基于另一个模型的字段在模型上创建动态属性的方法,我认为这是一种模式 – 少数据库将是一个不错的选择。 所以,例如,我有模型: class Account include Mongoid::Document field :name, :type => String field :token, :type => String field :info_needed, :type => Array embeds_many :members end class Member include Mongoid::Document embedded_in :account, :inverse_of => :members end 我希望获取Account模型的“info_needed”属性,并根据内部的内容在Member模型上创建动态属性。 如果club.info_needed是[“first_name”,“last_name”],我正在尝试创建一个将first_name和last_name属性保存到Member模型的表单。 但是,在练习时,我只是在尝试执行此操作时不断在Member模型上获得“undefined method first_name =”错误。 我知道MongoDB可以处理每个记录的动态属性,但是如何在没有未定义的方法错误的情况下让Mongoid执行此操作?

有to_json返回一个mongoid作为字符串

在我的Rails API中,我希望Mongo对象作为JSON字符串返回,其中Mongo UID作为“id”属性而不是“_id”对象。 我希望我的API返回以下JSON: { “id”: “536268a06d2d7019ba000000”, “created_at”: null, } 代替: { “_id”: { “$oid”: “536268a06d2d7019ba000000” }, “created_at”: null, } 我的型号代码是: class Profile include Mongoid::Document field :name, type: String def to_json(options={}) #what to do here? # options[:except] ||= :_id #%w(_id) super(options) end end

Mongoid Group By或MongoDb group by in rails

我有一个mongo表,有如下统计数据…. COURSE_ID 状态which is a string, played or completed 和使用Mongoid的时间戳function的时间戳信息 所以我的class级如下…… class Statistic include Mongoid::Document include Mongoid::Timestamps include Mongoid::Paranoia field :course_id, type: Integer field :status, type: String # currently this is either play or complete 我想每天获得一个课程总数的数量。 所以例如… 8/1/12有2个剧本,8/2/12有6个剧本。 等等。因此我将使用created_at时间戳字段,使用course_id和action。 问题是我没有看到Mongoid中的group by方法。 我相信mongodb现在有一个,但我不确定如何在rails 3中完成。 我可以使用每一个来遍历表格,并在轨道中加入一些地图或散列,但是如果课程有100万个视图,那么检索和迭代超过一百万个记录会很麻烦。 有干净的方法吗?

按日期范围查询Mongoid

我如何写两个日期范围的查询? 唯一的条件是必须通过一个查询检索此数据。 谢谢。 UPD:或如何在一个集合中结合2个查询? 不是数组

Mongoid批量更新/ Upsert替代?

我知道Mongoid v3 +支持通过Model.collection.insert()批量插入。 但是,我不认为它支持批处理更新,其中每个记录的属性不同(所以我不认为update_all也可以工作)。 有没有办法进行批量更新/ upsert而不是单记录查找和更新? 这是一个简化的例子,我有2个模型: class Product … has_and_belongs_to_many :lists end class List … has_and_belongs_to_many :products end 创建新Product ,我将其与一个或多个Lists相关联。 但是,我还需要每天更新Product属性而不会丢失List参考信息(我确实没有运行Product上的validation)。 不使用批处理的一种方法是在Product上调用find_or_initialize_by并更新属性。 但对10K-1M +记录这样做是非常耗时的。 另一种使用批量插入的方法是执行Product.delete_all ,然后执行Product.collection.insert(…) ,但这会创建新的product_ids并且不再维护与List的关系。 在这个例子中有没有办法进行批量更新或upsert?

将MongoDB中的_id类型更改为整数是不是很糟糕?

MongoDB对_id使用ObjectId类型。 如果我使_id为递增整数会不会很糟糕? (有了这个gem,如果你有兴趣)

在Mongoid中使用集合级操作

我正在试验Ruby(我不太清楚)和Mongo(我这样做)。我用一个:accessed access字段创建了一个Mongoid模型。 我知道在Mongo我可以运行如下: data = db.collection.findAndModify({ query: { … }, update: {$inc: {accessed: 1}} }) 但是当我在MyModel.collection.find_and_modify中运行MyModel.collection.find_and_modify时,我会回到看似哈希的东西。 有没有办法可以将其强制转换为我的模型类的实例,或者在Mongoid中执行更好的支持查询?

Rails / Mongoid:在mongoimport之后,父对象无法识别has_many / belongs_to关系的子对象

使用mongoimport工具通过CSV将包含以下行的CSV导入mongodb: object_1_id,field1,field2 52db7f026956b996a0000001,apples,oranges 52db7f026956b996a0000001,pears,plums 这些字段将导入到集合Object2 。 导入后,通过控制台确认存在行。 # # Object2可以通过object_1_id访问object_1_id : > o = Object2.first # > o1 = o.object_1 # 但是Object1看不到任何使用mongoimport导入的Object2行。 它可以看到通过控制台或其他方式创建的所有行: > o1.object_2s.count 10 > o1.object_2s.find(“52e0713417bcabcb4d09ad12”) Mongoid::Errors::DocumentNotFound: Document not found for class Object2 with id(s) 52e0713417bcabcb4d09ad12. TL; DR Object1似乎无法识别通过mongoimport导入的子模型,尽管子项正确存储了父ID并且能够识别其父项。

轨道上的ruby中的嵌套属性

我想在ruby中创建评论,但我有问题 1)posts_controller.rb def comment Post.find(params[:id]).comments.create(params[:comment]) flash[:notice] = “Added your comment” redirect_to :action => “show”, :id => params[:id] end 2)show.html.erb “comment”, :id => @post %> 3)post.rb class Post include Mongoid::Document include Mongoid::Timestamps::Created include Mongoid::Timestamps::Updated field :title, type: String field :content, type: String field :user_id, type: Integer field :tag, type: String field :owner, type: String embeds_many :comments […]

Ruby on Rails – 如何从嵌套模型委派错误消息

class User include Mongoid::Document has_many :images accepts_nested_attributes_for :image end class Image include Mongoid::Document include Mongoid::Timestamps include Mongoid::Paperclip has_mongoid_attached_file :uploaded_image, :path => “:rails_root/public/uploads/:class/:id/:basename.:extension”, :url => “public/uploads/” validates_attachment_content_type :uploaded_file, :content_type => “application/png”, :message => “error massage” belongs_to :user delegate :url, :path, to: :uploaded_image, allow_nil: true, prefix: false end 如果:uploaded_image无效,如何将Image中的错误委托给用户? 例如: user_image = user.images.build(uploaded_image: new_image.path) user_image.save 如果uploaded_image无效,应该引发错误。