MongoDB 中的嵌套注释 [英] Nested Comments in MongoDB

查看:24
本文介绍了MongoDB 中的嵌套注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 MongoDB 很陌生,正在尝试用它构建一个嵌套的评论系统.在网上,您可以找到各种文档结构来实现这一点,但我正在寻找一些建议,使我能够轻松地通过评论执行以下操作

I'm quite new to MongoDB and trying to build a nested comment system with it. On the net you're finding various document structures to achieve that, but I'm looking for some proposals that would enable me easily to do the following things with the comments

  • 将评论标记为垃圾邮件/已批准并通过此属性检索评论
  • 检索用户评论
  • 检索对象/用户的评论数

当然,除了像通常那样显示评论之外.如果您对如何使用 MongoDB 处理这些事情有任何建议 - 或者 - 告诉我寻找替代方案,我们将不胜感激!

Besides of course displaying the comments as it is normally done. If you have any suggestions on how to handle these things with MongoDB - or - tell me to look for an alternative it'd be appreciated much!

推荐答案

您是否考虑过将注释存储在所有需要引用它们的文档中?如果您有该用户的文档,请在其中存储该用户的所有评论.如果您有一个单独的对象文档,请将所有注释也存储在那里.来自关系世界后感觉有点错误,您尝试拥有给定数据的完全副本,然后通过 ID 引用它,但即使使用关系数据库,如果您希望运行查询,也必须开始复制数据很快.

Have you considered storing the comments in all documents that need a reference to them? If you have a document for the user, store all of that user's comments in it. If you have a separate document for objects, store all comments there also. It feels sort of wrong after coming from a relational world where you try to have exactly one copy of a given piece of data, and then reference it by ID, but even with relational databases you have to start duplicating data if you want queries to run quickly.

使用这种设计,您加载的每个文档都将是完整的".它将拥有您需要的所有数据,并且该集合上的索引将保持快速读取.代价是写入速度会稍微慢一些,而且当您需要更新评论文本时更令人头疼,因为您需要更新多个文档.

With this design, each document that you load would be "complete". It would have all the data you need, and indexes on that collection would keep reads fast. The price would be slightly slower writes, and more of a headache when you need to update the comment text, since you need to update more than one document.

这篇关于MongoDB 中的嵌套注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆