使用 Mongoid 从数据库延迟下载字段,或处理大型文档

发布于 2024-12-27 15:42:20 字数 354 浏览 0 评论 0原文

我们将 Mongo 数据库设计为高度非规范化,导致我们集合中的许多文档包含非常大的数组作为某些字段。当然,这可能会导致从我们的数据库下载的时间超过所需的时间,因为文档太大了。

每当我们需要从数据库中获取一些记录时,我都会通过使用 .only 来选择我想要的字段来减轻性能影响,但这需要我在需要之前下载额外的数据,一般来说它是当我查询文档时,我需要更多的精力来跟踪最终需要哪些字段。

Mongoid 是否有一种方法可以让我简单地将模型中的特定字段定义为应该延迟加载的字段,以便我在第一次访问它们时从服务器获取它们?我搜索了 Mongoid 的文档,看看它是否有内置的东西,但我没有看到任何这样的东西。也许有第三方 gem 可以将此功能添加到 Mongoid 中?

We've designed our Mongo database to be highly denormalized, resulting in many documents in our collections containing very large arrays as some of the fields. Naturally, this can result in downloads from our DB longer than necessary because the documents are just so large.

Whenever we need to grab some records from the DB, I have mitigated the performance implications of this by using .only to choose just the fields I want, but this requires me to download that extra data before I may need it, and in general it's a lot more involved for me to keep track of what fields end up being needed when I am querying for the document(s).

Does Mongoid have a way that I can simply define particular fields in my model as ones that should be lazily loaded so that I grab them from the server just when they're first accessed? I searched through Mongoid's documentation to see if it had anything built in, but I'm not seeing any such thing. Perhaps there's a third party gem that adds this functionality to Mongoid?

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

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

发布评论

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

评论(1

等数载,海棠开 2025-01-03 15:42:20

Mongoid 不支持从服务器延迟加载数据,也不知道有任何插件可以做到这一点。

虽然从技术上讲您可以将其添加到 Mongoid,但您仍然最好手动指定only,这样您就可以加载所需的内容一次。如果您根据使用情况延迟加载字段,则每次在 nil 时访问字段时,您都必须从 MongoDB 中提取数据。

这意味着,如果您在原始文档加载的基础上访问了 5 个不同的字段,那么您将向 MongoDB 发送 6 个查询,这涉及一般的往返/处理,而不是首先在 only 中指定它。

Mongoid doesn't have support for lazy loading data from the server, not aware of any plugins to do it either.

While technically you could add this to Mongoid, you would still be better off manually specifying only so you load what you need once. If you lazy loaded the fields based on usage, you would have to pull the data from MongoDB every time a field is accessed while nil.

Meaning if you accessed 5 different fields on top of the original document load, you're sending 6 queries to MongoDB which involves the general roundtrip/processing as compared to just specifying it in only in the first place.

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