MongoMapper 到 Mongoid:如何在数组内搜索?
这里的“self”代表接触模型。 此查询将查找联系人的所有已完成任务。 任务模型有一个数组类型的字段/键:assigned_contacts。 所以查询是在数组内部搜索。
def assigned_tasks_completed
self.company.tasks.all(:assigned_contacts => self.id.to_s, :completed => true)
end
如何在 Mongoid 中做到这一点?
另一个问题: Mongoid 中 @contact.set(:a -> a, :b -> b, :c -> c) 的等价物是 update_attributes!
但Mongoid中也设置了一个方法。 set 和 set 和有什么不一样?更新属性!在蒙古人?
"self" here stands for Contact model.
This query will find all completed tasks for a contact.
The Task model has a field/key :assigned_contacts of type Array.
So the query is searching inside the Array.
def assigned_tasks_completed
self.company.tasks.all(:assigned_contacts => self.id.to_s, :completed => true)
end
How to do this in Mongoid?
Another question:
The equivalent in Mongoid of @contact.set(:a -> a, :b -> b, :c -> c) is update_attributes!
But there is also a method set in Mongoid.
What is the difference between set & update_attributes! in Mongoid ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您的任务是一个单独的文档。因此,您只需将
all
替换为where
即可,对于您的另一个问题,
两者都是 mongoid 设置 & 更新属性在内部使用mongodb $set。但不同的是,mongoid set 只接受单个字段更新,而 update_attributes 接受多个字段。
I assume your tasks is a separate document. So you can just replace
all
withwhere
, it will workFor your another question,
Both mongoid set & update attributes are internally uses mongodb $set. But the difference is mongoid set is only accepts single field update, update_attributes accepts multiple.