1

所以我从 SQL 背景切换到 NoSQL。所以我知道我应该在这里“去规范化”。所以基本上我对我必须做什么有一个简化的想法;

用户 这些文件包含身份验证信息,可能是付款方式、用户名和各种详细信息

帖子 这些帖子是由用户发布的,在每个帖子中,我们必须显示用户的用户名和电子邮件。因此,通过“非规范化”的方法,我会将用户的用户名和电子邮件放入她/他发布的每个帖子中。

但是,当用户更改他们的用户名或电子邮件时,这不会造成问题吗?我不需要追溯更新所有帖子吗?

我是否正确设计了这个?还是错过了什么?

4

1 回答 1

2

在这种情况下,您没有义务对数据进行非规范化。

您可以将用户和帖子存储为单独的文档(在同一个数据库中):

{
  "_id":"alice",
  "email":"alice@wonderland.org"
}

{
  "author":"alice",
  "text":"Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversation?'"
}

然后你需要一个映射函数来告诉哪个字段将用于连接:

function(o) {
   if (o.text) {
     emit(o._id, {text:o.text, _id:o.author});
   }
}

然后调用此视图,include_docs=true并将帖子的 ID 称为key.

也许为此创建一个索引有点过头了,但是您可以将它重用于其他有用的东西(例如“整理”博客文章和评论)。

于 2013-10-17T17:25:59.303 回答