MongoMapper 到 Mongoid:如何在数组内搜索?

发布于 2024-12-23 03:30:39 字数 421 浏览 1 评论 0原文

这里的“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好久不见√ 2024-12-30 03:30:39

我假设您的任务是一个单独的文档。因此,您只需将 all 替换为 where 即可,

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

对于您的另一个问题,

两者都是 mongoid 设置 & 更新属性在内部使用mongodb $set。但不同的是,mongoid set 只接受单个字段更新,而 update_attributes 接受多个字段。

I assume your tasks is a separate document. So you can just replace all with where, it will work

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

For 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文