与 NoSQL 数据库创建关系的有效方法
我目前正在尝试实现类似于 Tumblr 的用户交互,例如我当前关注的人的转发、关注、关注者、评论、博客文章等。 此外,还需要显示每篇博客文章的活动。
我一直致力于为数据库创建正确的模式。有多种方法可以实现这种功能(定义嵌入的数据结构,如博客文章和评论、为每个操作创建活动文档等),但我目前无法确定哪种方法在性能和可扩展性方面是最好的。
例如,让我们看看我关注的人的实现。这是示例用户文档。
User = { id: Integer,
username: String,
following: Array of Users,
followers: Array of Users,
}
这看起来微不足道。我可以管理每个用户操作的以下字段(关注/取消关注),但是如果我当前关注的用户被删除怎么办?更新关注已删除用户的所有用户记录是否有效。
另一个问题是创建我关注的人的博客文章视图。
Post = { id: Integer,
author: User,
body: Text,
}
那么它是否有效查询最新帖子,例如;
db.posts.find( { author: { $in : me.followers} } )
I am currently trying to implement Tumblr-like user interactions like reblog, following, followers, commenting, blog posts of people who I currently following etc.
Also there is a requirement to display activity for each blog post.
I am stuck with creating proper schema for database. There are several way to achieve this kind of functionality (defining data structures embedded like blog posts and comments, creating an activity document for each action etc.) but I couldn't currently decide which way is the best in terms of performance and scalability.
For instance let's look at implementation of people who I follow. Here is sample User document.
User = { id: Integer,
username: String,
following: Array of Users,
followers: Array of Users,
}
This seems trivial. I can manage following field per user action (follow/unfollow) but what if an user who I currently follow is deleted. Is it effective to update all User records who follows deleted user.
Another problem is creating a view of blog post from people who I follow.
Post = { id: Integer,
author: User,
body: Text,
}
So is it effective query latest posts like;
db.posts.find( { author: { $in : me.followers} } )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(在我看来)您似乎正在尝试使用单个数据存储(在本例中是面向文档的 NoSQL 数据库)来满足(至少)两个不同的要求。您似乎要做的第一件事是将数据存储在面向文档的存储中。我假设您这样做有正当理由。
您似乎要做的第二件事是在您存储的文档之间建立关系。您的示例显示了 FOLLOWS 关系。我建议将其视为与在面向文档的 NoSQL 数据库中存储数据不同的要求,并考虑将关系存储在面向图形的 NoSQL 数据库(例如 Neo4j)中。这样,您的实体可以存储在文档存储中,关系可以仅使用文档 ID 存储在图形存储中。
我的经验是,很难(如果不是不可能的话)使用单个 NoSQL 数据库来满足中型到大型应用程序的所有功能和非功能需求。例如,我正在开发的最新应用程序除了 RDBMS 之外还使用 MongoDB、Redis 和 Neo4j。我花了很多时间尝试技术并确定了这种组合。我致力于使用 Spring 3 以及 Spring Data 项目,到目前为止我的经验非常丰富。
It seems (to me) that you are trying to use a single data store (in this case a document-oriented NoSQL database) to fulfill (at least) two different requirements. The first thing you seem to be trying to do is store data in a document-oriented store. I am going to assume that you have legitimate reasons for doing this.
The second thing you seem to be trying to do is establish relationship(s) between the documents you are storing. Your example shows a FOLLOWS relationship. I would recommend treating this as a different requirement from storing data in a document-oriented NoSQL database and look at storing the relationships in a graph-oriented NoSQL database such as Neo4j. This way, your entities can be stored in the document store and relationships in the graph store using just the document IDs.
My experience has been that it will be difficult (if not impossible) to get a single NoSQL database to meet all functional and non-functional needs of a medium to large sized application. For example, the latest application I am working on uses MongoDB, Redis and Neo4j besides an RDBMS. I spent a lot of time experimenting with technologies and settled on this combination. I have committed myself to using Spring 3, along with the Spring Data project and so far my experience has been great.
一种有效的方法称为“星型模式”。如果您搜索网络或维基百科,您会发现很多信息。
One approach that works is called "Star Schema". If you search the web or wikipedia then you'll find lots of information.