mongodb模式设计集合与索引

标签 mongodb

我只是在工作中尝试使用 mongodb/redis。我正在考虑这种情况,例如假设一个博客。

我计划主要进行以下查询:

  1. 列出特定用户的帖子
  2. 列出特定用户的评论
  3. 根据特定类别列出帖子
  4. 查看单个帖子及其评论

注意:Redis用于存储userid<->用户名映射。

这是一个良好/灵活的架构设计吗? future 的计划是添加帖子/评论评级。

Post

  • _id
  • author (db-ref: user._id; index)
  • content
  • category (index)

User

  • _id
  • username (unique index)
  • passwordhash

Comments

  • _id
  • post (db-ref: post._id; index)
  • author (db-ref: author._id; index)

添加评分/投票

Post

  • _id
  • author (db-ref: user._id; index)
  • content
  • category (index)
  • votes (number)

User

  • _id
  • username (unique index)
  • passwordhash

Comments

  • _id
  • post (db-ref: post._id; index)
  • author (db-ref: author._id; index)
  • votes (number)

CommentVotes

  • _id = author.Id (key)
  • voted: [comment._id, comment._id, ...]

PostVotes

  • _id = author.Id (key)
  • voted: [post._id, post._id]

你觉得怎么样?或者我只是疯了?

最佳答案

我认为你仍然在进行关系性思考。我喜欢将文档数据对象视为带有链接(线程?)的俄罗斯娃娃,可以将一个娃娃链接到另一个娃娃。对于您的情况:

  • 用户娃娃
    • 用户名
    • 电子邮件
    • 密码哈希
    • PostUpvotes(帖子 ID 列表)
    • CommentUpvotes(评论 ID 列表)
  • 帖子娃娃
    • 评论娃娃(评论列表)
      • 评论
        • 用户 ID
        • 用户名
        • 总票数(整数)
        • 评论 ID
    • 总票数(整数)
    • 类别 ID(列表)(一篇帖子肯定可以有多个吗?)
    • 作者 ID
  • 类别娃娃

您可能希望将对评论和帖子的投票放入用户对象中,然后使用 map/reduce 每隔 15 分钟左右获取一次计数(这是 map/reduce 的“规范”使用)。关于赞成票的重要一点是,用户不能重复投票,因此将它们保留在 User 对象中更有意义(无需点击索引来查看用户是否已经对帖子投票)。由于每个帖子的实际点赞数并不那么重要,因此可以将其保留在帖子中并定期更新。

关于mongodb模式设计集合与索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11781436/

相关文章:

mongodb - Reactivemongo 将 map 序列化为 BSONDocument

mongodb - 错误 16755 - Mongo 无法提取地理键

javascript - mongodb 更新 JSON 数组

node.js - Mongo NodeJS project() 不是一个函数

MongoDB:查询特定元素序列的数组

node.js - 匹配 Mongoose 中的两个不同字段,聚合?

javascript - meteor 黑客 :aggregate gives "MongoError: A pipeline stage specification object must contain exactly one field."

json - Mongoose 返回空对象,在 mongo shell 中工作

mongodb - 不了解 mongoDB 更新如何在 Go 中工作

node.js - 如何高效/高性能地对多个集合进行多次查找和更新?