我正在 Node.js + MongoDB 数据库中创建一个博客应用程序。我以前使用过 MySQL 等关系数据库,但这是我第一次体验 NoSQL 数据库。因此,在进一步行动之前,我想先确认我的 MongoDB 数据模型。
我决定我的 blogDB 有 3 个集合
- post_collection - 存储有关该文章的信息
- comment_collection - 存储有关文章评论的信息
- user_info_collection - 包含用户信息
PostDB
{
_"id" : ObjectID(...),
"author": "author_name",
"Date": new Date(....),
"tag" : ["politics" , "war"],
"post_title": "My first Article",
"post_content": "Big big article"
"likes": 23
"access": "public"
}
CommentDB
{
"_id" : Objectid(...),
"POST": "My First Article",
"comment_by": "User_name",
"comment": "MY comments"
}
UserInfoDB
{
"_id": ObjectID(...),
"user": "User_name",
"password": "My_password"
}
非常感谢您的评论。
最佳答案
在您的位置,我会将 Comments 集合嵌入到 Posts 集合中。 这样做的缺点是,如果您有很多评论,则可能会达到 16MB 的限制。 优点是预先连接了数据,因此查询速度会更快。无论如何,您不会在没有帖子的情况下寻找评论。
另一件事是,您可以将“user”字段设为 UserInfo 集合的 ID。这样您就可以在评论中引用唯一用户。
关于node.js - 了解 NoSQL 数据建模 - 博客应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17289900/