Nosql数据库设计-MongoDB

akc*_*soy 5 database-design mongodb nosql

我正在尝试构建一个只有以下3种模型的应用程序:

  • topic (has just a title (max 100 chars.))
  • comment (has text (may be very long), author_id, topic_id, createdDate)
  • author (has just a username)

Actually a very simple db structure. A Topic may have many comments, which are created by authors. And an author may have many comments.

I am still trying to figure out the best way of designing the database structure (documents). First I though to put everything to its own schema like above. 3 Documents. But since this is a nosql db, I should actually try to eliminate the needs for a join. And now I am really thinking of putting everything to a single document, which also sounds crazy.

These are my actually queries from ui:

  • Homepage query: Listing all the topics, which have received the most comments today (will run very often)
  • Auto suggestion list for search field: Listing all the topics, whose title contains string "X"
  • Main page of a topic query: Listing all the comments of a topic, with their authors' username.

Since most of my queries need data from at least 2 documents, should I really just use them all together in a single document like this:

Comment (text, username, topic_title, createdDate)

This way I will not need any join, but also save i.e. the title of topics multiple times.. in every comment..

I just could not decide.

I appreciate any help.

小智 1

您可以进行您建议的第二种设计,但这一切都取决于您想要如何使用数据。我假设您\xe2\x80\x99 将把它用于网站。

\n\n

如果您希望评论可点击,点击主题名称将重定向到主题\xe2\x80\x99s页面,或者单击用户名将重定向到用户\xe2\x80\x99s页面,您可以在其中查看所有评论他的评论,我建议你保留它们作为ID。因为您可以稍后使用.populate(\xe2\x80\x9cfield1 field2\xe2\x80\x9d),并且可以选择您想要从该 ID 获取的字段。

\n\n

或者,您可以将 topic_name 和用户名及其 ID 存储在同一文档中以减少查询,但最终会存储更多冗余数据。

\n