【问题标题】:MongoMapper to Mongoid: How to search inside an array?MongoMapper 到 Mongoid:如何在数组中搜索?
【发布时间】:2011-12-24 13:10:44
【问题描述】:

这里的“self”代表Contact模型。 此查询将查找联系人的所有已完成任务。 任务模型有一个数组类型的字段/键:assigned_contacts。 所以查询是在数组内部搜索。

def assigned_tasks_completed
  self.company.tasks.all(:assigned_contacts => self.id.to_s, :completed => true)
end

如何在 Mongoid 中做到这一点?

另一个问题: @contact.set(:a -> a, :b -> b, :c -> c) 在 Mongoid 中的等价物是 update_attributes!

但是在Mongoid中也有设置方法。 set 和 update_attributes 有什么区别!在 Mongoid 中?

【问题讨论】:

    标签: ruby mongodb mongoid mongomapper


    【解决方案1】:

    我假设您的任务是一个单独的文档。所以你可以用where替换all,它会起作用

    def assigned_tasks_completed
      self.company.tasks.where(:assigned_contacts => self.id.to_s, :completed => true)
    end
    

    关于您的另一个问题,

    mongoid setupdate attributes 在内部使用 mongodb $set。但不同的是 mongoid set 只接受单个字段更新,update_attributes 接受多个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      相关资源
      最近更新 更多