博客的mongodb模式设计 [英] mongodb schema design for blogs

查看:111
本文介绍了博客的mongodb模式设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用基于文档的数据库(mongodb)为类似博客的站点设计架构。该网站具有以下对象:用户,文章,评论。用户可以添加评论文章。每个用户也可以每次评论一次。

How would you design the schema for a blog-like site with document-based databases (mongodb). The site has the following objects: User, Article, Comment. User can add Comments to Article. Each User can also vote exactly once per Comment.

我想要有效地执行这些查询:

1.获取文章A,评论关于文章A和每个评论的投票数

2.获取所有文章中的用户B的所有评论

3.获取所有评论用户B投票支持

I want to be able to do these queries efficiently:
1. get Article A, comments on Article A and # of votes per comments
2. get all comments by User B across all articles
3. get all comments User B voted for

我的第一个尝试是将文章和评论放在单独的集合中,评论可以包含投票的用户列表。这使得查询1和2简单。而对于3,我添加了Vote集合,用户可以跟踪投票。

My first attempt is to put articles and comments in separate collections and comment can contain a list of users that voted for it. This makes query 1 and 2 simple. And for 3, I added Vote collection which keep tracks of votes by users.

有一些明显的缺点,如复制用户投票数据和查询1将对数据库进行两次调用。有没有更好的方法?

There's some obvious drawback such as duplicating user vote data and query 1 will take two calls to the database. Is there a better approach?

Article {
  "user_id"
}

Comment {
   "user_id",
   "article_id",
   [user_voted],
}

Vote {
    "user_id",
    "comment_id",
}


推荐答案

Article {
  "_id" : "A",
  "title" : "Hello World",
  "user_id" : 12345,
  "text" : 'My test article',

  "comments" : [
    { 'text' : 'blah', 'user_id' : 654321, 'votes' : [987654]},
    { 'text' : 'foo', 'user_id' : 987654, 'votes' : [12345, 654321] },
    ...
  ]
}

这里的基本前提是我已经嵌套了 code>里面的文章投票仅适用于评论,因此他们已经以每个评论。在这种情况下,我刚刚存储了user_id。如果你想存储更多的信息(time_created等),那么你可以投票一个对象数组:

The basic premise here is that I've nested the Comments inside of the Article. The Votes only apply to a Comment, so they've been stored as an array with each Comment. In this case, I've just stored the user_id. If you want to store more information (time_created, etc.), then you can votes an array of objects:

... 'votes' : [ { user_id : 987654, ts : 78946513 } ] ...

如何执行您的查询有效:

How to perform your queries efficiently:



  1. 获取文章A,对每条评论的A条和投票数的评论




db.articles.find( { _id : 'A' } )

这样就可以获得一个查询。您可能需要做一些客户端逻辑来计算每个评论的投票数,但这很简单。

This gets everything with one query. You may have to do some client-side logic to count votes per comment, but this is pretty trivial.



  1. 获取所有文章中的用户B的所有评论




db.articles.ensureIndex( { "comments.user_id" : 1 } )
db.articles.find( { "comments.user_id" : 987654 } ) // returns all document fields

索引将允许有效地搜索文档中的评论。

The index will allow for efficiently searching the comments within a document.

有目前没有办法从子数组中只提取匹配项。这个查询实际上会返回所有的用户的评论文章。如果这可能是太多的数据,可以进行一些修整。

There's currently no way to extract only the matches from a sub-array. This query will in fact return all of the articles with comments by that user. If this is potentially way too much data, you can do some trimming.

db.articles.find( { "comments.user_id" : 987654 }, { "title" : 1, "comments.user_id" : 1 })





  1. 获取所有评论用户B投票




db.articles.ensureIndex( { "comments.votes" : 1 } )
db.articles.find( { "comments.votes" : 987654 } )

再次,这将返回所有文章,而不仅仅是评论。

Again, this will return all of the Articles, not just the comments.

这里有一个权衡。返回文章可能看起来像是带回了太多的数据。但是,当您查询#3时,您打算向用户显示什么?

There's a trade-off to be made here. Returning the article may seem like we're bringing back too much data. But what are you planning to display to the user when you make query #3?

获取我已投票的评论列表

Getting a list of "comments I've voted for" is not terribly useful without the comment itself. Of course the comment is not very useful without the article itself (or at least just the title).

大多数时候,查询#3从投票评论文章。如果是这样,那么为什么不把文章带回来呢?

Most of the time, query #3 devolves into a join from Votes to Comments to Articles. If that's the case, then why not just bring back the Articles to start with?

这篇关于博客的mongodb模式设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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