存储嵌入注释与避免 MongoDB 中的开销
让我解释一下我的问题,希望有人能提供一些好的建议。
我目前正在开发一个网络应用程序,用于存储大量应用程序的信息和元数据。对于每个应用程序,可能有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议更具体地考虑您的查询 - 您指定要返回对象的哪些部分。当您只对显示有关应用程序的某些特定信息感兴趣时,这应该可以避免获得一堆嵌入注释的开销。
您可以执行以下操作:
db.collection.find({ appName : 'Foo'}, {comments : 0 });
使用appName Foo
检索应用程序对象,但要特别排除嵌入其中的comments
对象(更可能是对象数组)。来自 MongoDB 文档
编辑
另请记住
limit(n)
函数一次仅检索n
个应用程序。例如,获取没有评论的n=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 withappName Foo
, but specifically exclude thecomments
object (more likely array of objects) embedded within it.From the MongoDB docs
EDIT
Also remember the
limit(n)
function to retrieve onlyn
apps at a time. For instance, gettingn=50
apps without their comments would be: