在 Mongoid 中缓存引用文档的字段的更好方法是什么?

发布于 2024-12-22 11:45:12 字数 426 浏览 2 评论 0原文

在 Mongoid 中缓存引用文档的字段的更好方法是什么?

目前我为此使用其他字段:

class Trip
  include Mongoid::Document

  belongs_to :driver

  field :driver_phone
  field :driver_name

end

class Driver
  include Mongoid::Document

  field :name
  field :phone
end

可能将缓存存储为嵌套对象会更清楚,因此在 mongo 中它将存储为:

{ driver_cache: { name: "john", phone: 12345 } }

我考虑过具有 1-1 关系的嵌入式文档吗?这是正确的选择吗?

What is the better way to cache fields of referenced document in Mongoid?

Currently I use additional fields for that:

class Trip
  include Mongoid::Document

  belongs_to :driver

  field :driver_phone
  field :driver_name

end

class Driver
  include Mongoid::Document

  field :name
  field :phone
end

May be it would be more clear to store cache as nested object so in mongo it would be stored as:

{ driver_cache: { name: "john", phone: 12345 } }

I thought about embedded document with 1-1 relation? Is that right choice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

苦行僧 2024-12-29 11:45:12

Mongoid (Durran Jordan) 的作者建议以下选项

这个宝石看起来很适合这种类型的东西:

https://github.com/logandk/mongoid_denormalize

Author of Mongoid (Durran Jordan) suggested folowing option

This gem looks handy for this type of thing:

https://github.com/logandk/mongoid_denormalize

两仪 2024-12-29 11:45:12

很好的问题。我创作并维护了 mongoid_alize,这是一个用于非规范化 mongoid 关系的 gem。所以我对这个问题很挣扎。

从 0.3.1 开始,alize 现在将非规范化一对一的数据存储在哈希中,非常类似于上面带有 driver_cache 的第二个示例。然而,以前的版本将数据存储在单独的字段中(您的第一个示例)。

这一变化是由许多因素推动的,但主要是为了使一对一和一对多的处理保持一致(alize 可以非规范化一对一、一对多和多对多) 。一对多总是通过将数据存储在哈希数组中来处理。因此,将一对一存储为哈希就成为一种更加对称的设计。

它还解决了其他几个问题。您可以在这里找到更详细的解释 - https://github.com/dzello/mongoid_alize#release- 030

希望有帮助!

Great question. I authored and maintain mongoid_alize, a gem for denormalizing mongoid relations. And so I struggled with this question.

As of 0.3.1 alize now stores data for a denormalized one-to-one in a Hash, very much like your second example above w/ driver_cache. Previous versions, however, stored data in separate fields (your first example).

The change was motivated by many factors, but largely to make the handling of one-to-one's and one-to-many's consistent (alize can denormalize one-to-one, one-to-many, and many-to-many). one-to-many's have always been handled by storing the data in a array of hashes. So storing one-to-one's as just a Hash becomes a much more symmetrical design.

It also solved several other problems. You can find a more detailed explanation here - https://github.com/dzello/mongoid_alize#release-030

Hope that helps!

说谎友 2024-12-29 11:45:12

不管怎样,你都很好。

第一种方法似乎稍微好一些,因为它明确说明了您正在缓存的数据。而且,它(可能)需要 Mongoid 做更少的工作:-)

Either way, you're fine.

First approach seems slightly better because it explicitly states what data you're caching. Also, it (probably) requires less work from Mongoid :-)

凹づ凸ル 2024-12-29 11:45:12

Alexey,

我建议考虑如何使用数据。如果您始终在行程对象的上下文中使用驾驶员信息,那么嵌入可能是正确的选择。

但是,如果您将在其他上下文中使用该信息,也许它会更好,因为它是您创建的自己的集合。

另外,请考虑将行程嵌入驱动程序对象内。考虑到您的应用程序正在尝试执行的操作,这可能有意义,也可能没有意义,但从逻辑上讲,拥有一组驱动程序,每个驱动程序都有一组行程(嵌入或未嵌入)而不是嵌入驱动程序的行程是有意义的。我可以看到这种情况(每次旅行总是在司机的背景下进行)比上述情况更常见。

-泰勒

Alexey,

I would recommend thinking about how the data will be used. If you always use the driver information in the context of a trip object then embedding is probably the proper choice.

If, however, you will use that information in other contexts perhaps it would be better as it's own collection as you have created it.

Also, consider embedding the trips inside the driver objects. That may or may not make sense given what your app is trying to do but logically it would make sense to have a collection of drivers that each have a set of trips (embedded or not) instead of having trips that embed drivers. I can see that scenario (where a trip is always though of in the context of a driver) to be more common than the above.

-Tyler

情丝乱 2024-12-29 11:45:12

可缓存数据的替代哈希存储:(

field :driver_cache, type: Hash

请记住,在内部密钥将转换为字符串。)

Alternative hash storage of your cacheable data:

field :driver_cache, type: Hash

(Keep in mind, internally the keys will be converted to strings.)

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