存储嵌入注释与避免 MongoDB 中的开销

发布于 2024-09-12 08:06:27 字数 821 浏览 4 评论 0原文

让我解释一下我的问题,希望有人能提供一些好的建议。

我目前正在开发一个网络应用程序,用于存储大量应用程序的信息和元数据。对于每个应用程序,可能有 10 到 100 条与该应用程序和应用程序版本 ID 相关的注释。我使用 MongoDB 是因为需要轻松的未来可扩展性和速度。我读过出于读取性能原因应将注释嵌入集合中,但我不确定这是否适用于我的情况。我在另一篇文章中读到:

一般来说,如果您需要单独处理给定的数据集,请将其设为集合。
作者:@kb

然而,就我而言,我不需要自己处理集合。让我进一步解释一下。我将有一个应用程序表(可以过滤),并在您滚动或过滤应用程序列表时动态加载条目。如果我将注释嵌入应用程序集合中,则当我将应用程序条目动态加载到表中时,我将发送所有注释。但是,我想做“延迟加载”,因为我只想在用户请求查看评论时加载评论(通过单击表中的条目)。

例如,我的表可能如下所示

| app name | version | rating | etc. | view comments |
------------------------------------------------------
| app1     | v.1.0   | 4 star | etc. | click me!     |
| app2     | v.2.4.5 | 3 star | etc. | click me!     |
| ...

我的问题是什么会更有效? MongoDB 上的读取速度是否足够快,以至于我提取每个应用程序的所有注释并不重要?如果用户没有过滤任何应用程序并一直滚动到底部,他们可能会加载 125k 到 250k 条目/应用程序。

Let me explain my problem, and hopefully someone can offer some good advice.

I am currently working on a web-app that stores information and meta-data for a large amount of applications. For each application there could be anywhere from 10 to 100's of comments that are tied to the application and an application version id. I am using MongoDB because of a need for easy future scalability and speed. I have read that comments should be embedded in a collection for read performance reasons, but I'm not sure that this works in my case. I read on another post:

In general, if you need to work with a given data set on its own, make it a collection.

By: @kb

In my case however I don't need to work on the collection by themselves. Let me explain further. I will have a table of apps (that can be filtered) and will dynamically load entries as you scroll, or filter, through the list of apps. If I embed the comments within the application collection, I am sending ALL the comments when I dynamically load the application entry into the table. However, I would like to do "lazy loading" in that I only want to load the comments when the user requests to see them (by clicking on the entry in the table).

As an example, my table might look like the following

| app name | version | rating | etc. | view comments |
------------------------------------------------------
| app1     | v.1.0   | 4 star | etc. | click me!     |
| app2     | v.2.4.5 | 3 star | etc. | click me!     |
| ...

My question is what would be more efficient? Are reads fast enough on MongoDB that it really doesn't matter that I am pulling all the comments with each application? If a user did not filter any of the applications and scrolled all the way to the bottom, they might load somewhere between 125k to 250k entries/applications.

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

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

发布评论

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

评论(1

蒲公英的约定 2024-09-19 08:06:27

我建议更具体地考虑您的查询 - 您指定要返回对象的哪些部分。当您只对显示有关应用程序的某些特定信息感兴趣时,这应该可以避免获得一堆嵌入注释的开销。

您可以执行以下操作: db.collection.find({ appName : 'Foo'}, {comments : 0 }); 使用 appName Foo 检索应用程序对象,但要特别排除嵌入其中的 comments 对象(更可能是对象数组)。

来自 MongoDB 文档

检索字段的子集
默认情况下,查找操作会返回整个文档/对象。但是,我们也可能要求仅返回某些字段。请注意,_id 字段始终会自动返回。

// select z from things where x=3
db.things.find( { x : 3 }, { z : 1 } );

您还可以删除您知道会很大的特定字段:

// get all posts about mongodb without comments
db.posts.find( { tags : 'mongodb' }, { comments : 0 } );

编辑
另请记住 limit(n) 函数一次仅检索 n 个应用程序。例如,获取没有评论的 n=50 应用程序将是:

db.collection.find({}, {comments : 0 }).limit(50);

I would suggest thinking more specifically about your query - you specify which parts of an object you'd like to return. This should allow you to avoid the overhead of getting a bunch of embedded comments when you're only interested in displaying some specific bits of information about the application.

You can do something like: db.collection.find({ appName : 'Foo'}, {comments : 0 }); to retrieve the application object with appName Foo, but specifically exclude the comments object (more likely array of objects) embedded within it.

From the MongoDB docs

Retrieving a Subset of Fields
By default on a find operation, the entire document/object is returned. However we may also request that only certain fields are returned. Note that the _id field is always returned automatically.

// select z from things where x=3
db.things.find( { x : 3 }, { z : 1 } );

You can also remove specific fields that you know will be large:

// get all posts about mongodb without comments
db.posts.find( { tags : 'mongodb' }, { comments : 0 } );

EDIT
Also remember the limit(n) function to retrieve only n apps at a time. For instance, getting n=50 apps without their comments would be:

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