Ruby on rails Mongoid-在一次调用中使用两个更新修饰符来更新mongodb中的文档

Ruby on rails Mongoid-在一次调用中使用两个更新修饰符来更新mongodb中的文档,ruby-on-rails,mongodb,mongoid,mongoid3,Ruby On Rails,Mongodb,Mongoid,Mongoid3,我正在尝试mongodb、mongoid和rails。我在Rails中有一个简单的任务和注释模型,其中注释嵌入到任务中。现在任务具有名为comment\u count的属性。是否有一种方法可以在一次调用中增加计数并将新注释推送到一起 1.9.3p194 :025 > task.comments.push(Comment.new(entry: "This is a comment")) => [#<Comment _id: 509e1708a490b3deed000003, _

我正在尝试mongodb、mongoid和rails。我在Rails中有一个简单的任务和注释模型,其中注释嵌入到任务中。现在任务具有名为comment\u count的属性。是否有一种方法可以在一次调用中增加计数并将新注释推送到一起

1.9.3p194 :025 > task.comments.push(Comment.new(entry: "This is a comment"))
 => [#<Comment _id: 509e1708a490b3deed000003, _type: nil, entry: "First comment">, #<Comment _id: 509e1716a490b3deed000004, _type: nil, entry: "Second comment">, #<Comment _id: 509e1aa3a490b3deed000005, _type: nil, entry: "This is a comment">] 
1.9.3p194 :026 > task.inc(:comment_count, 1)
 => 3 
任务模型:

class Task
  include Mongoid::Document
  field :name
  field :desc
  field :comment_count, type: Integer, default: 0
  embeds_many :comments
end
注释模型:

class Comment
  include Mongoid::Document
  field :entry
  embedded_in :task
end
下面是我想在一次调用中执行的操作

1.9.3p194 :025 > task.comments.push(Comment.new(entry: "This is a comment"))
 => [#<Comment _id: 509e1708a490b3deed000003, _type: nil, entry: "First comment">, #<Comment _id: 509e1716a490b3deed000004, _type: nil, entry: "Second comment">, #<Comment _id: 509e1aa3a490b3deed000005, _type: nil, entry: "This is a comment">] 
1.9.3p194 :026 > task.inc(:comment_count, 1)
 => 3 
1.9.3p194:025>任务.注释.推送(Comment.new(条目:“这是注释”))
=> [#, #, #] 
1.9.3p194:026>task.inc(:注释计数,1)
=> 3 
实际上,我打算在一次更新调用中使用多个更新修饰符,如$inc$push$pop等。类似于我们可以直接在mongo shell中执行的操作

请帮忙。
谢谢

不幸的是,Mongoid似乎不像ActiveRecord那样支持
计数器缓存


您可以在
注释
模型上使用
保存后
销毁后
回调来实现这一点,分别递增/递减父级计数器。

感谢您的回复。实际上,当前的练习看起来像是实现一个计数器缓存,但我实际上想要的是在一个更新调用中使用多个更新修饰符,如$inc、$push、$pop等。类似于我们可以直接在mongo shell中执行的操作。我会用这个更新我的问题。我不太了解mongoid,但这不是我的目的吗?