我需要关于 NoSQL/MongoDb 和数据/模型结构的建议 [英] I need an advice about NoSQL/MongoDb and data/models structure

查看:19
本文介绍了我需要关于 NoSQL/MongoDb 和数据/模型结构的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我正在探索 NoSQL 数据库.对于给定的问题,我需要关于如何以最优化和最有效的方式存储数据的建议.我现在的目标是 MongoDB.但是它应该与 CouchDB 相同.

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.

假设我们有这 3 个模型:

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:

  • 谁为这个故事投票?
  • 此用户投票支持什么?

在使用关系数据库时,我正在做简单的连接.问题是,我应该如何存储这些对象的数据才能最有效.

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.

例如,如果我将 Vote 对象存储为 Stories 的子集合,那么获取信息并不容易 - 用户投票支持的内容".

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".

推荐答案

我建议将投票存储为每个用户的故事 _id 列表.这样,您只需查看列表即可找出用户投票支持的故事.要获得为故事投票的用户,您可以执行以下操作:

I would suggest storing votes as a list of story _ids 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})

其中 story_id 是相关故事的 _id.如果您在 stories 字段上创建索引,这两个查询都会很快.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆