我需要一个关于NoSQL / MongoDb和数据/模型结构的建议 [英] I need an advice about NoSQL/MongoDb and data/models structure
问题描述
最近我在探索NoSQL数据库。我需要一个建议,如何以最优和有效的方式为给定的问题存储数据。我现在的目标是MongoDB。
假设我们有这3个模型:
故事:
id
标题
用户:
id
名称
投票:
id
story_id
user_id
我想要数据库这些问题:
- 谁投票了这个故事?
- ?
我在使用关系数据库时进行简单的连接。问题是,我应该如何存储这些对象的数据为了最有效率。
例如,如果我存储Vote对象作为一个子集的故事
我建议将投票存储为列表的故事 _id
在每个用户。这样,您可以通过查看列表来了解用户投票的故事。要让已投票支持故事的用户可以执行以下操作:
db.users.find({stories:story_id})
其中 story_id
是 _id
的问题。如果您在 stories
字段上创建索引,那么这两个查询都会很快。
Recently I'm exploring NoSQL Databases. I need an advice about how to store data in the most optimal and efficient way for a given problem. I'm targeting MongoDB, now. However it should be the same with CouchDB.
Let's say we have these 3 Models:
Story:
id
title
User:
id
name
Vote:
id
story_id
user_id
I want to be able to ask the database these questions:
- Who has voted for this Story?
- What this User has Voted for?
I'm doing simple joins while working with a relational DB. The question is, how should I store the data for those objects in order to be most efficient.
For example, if I store the Vote objects as a subcollection of Stories it wont be easy to get the info - "What a user has voted for".
I would suggest storing votes as a list of story _id
s in each user. That way you can find out what stories a user has voted for just by looking at the list. To get the users who have voted for a story you can do something like:
db.users.find({stories: story_id})
where story_id
is the _id
of the story in question. If you create an index on the stories
field both of those queries will be fast.
这篇关于我需要一个关于NoSQL / MongoDb和数据/模型结构的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!