用于存储“投票"的高效文档格式在 Mongo DB 中? [英] Efficient document format to store "Votes" in Mongo DB?

查看:49
本文介绍了用于存储“投票"的高效文档格式在 Mongo DB 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将投票"存储在 MongoDB 中,但我一直在思考如何以有效的方式进行.基本上,我有一个问题,有几个选项,如 A B C D ...(共 6 个).我让选民可以选择一个选项,并希望使用以下字段保存投票":MongoDate、选项、选民姓名,也许还有更多字段.

I'm trying to store "Votes" in MongoDB and I am stuck on how to proceed in an efficient way. Basically , I have a question with several options like A B C D ...(6 total). I am giving voters the option to choose an option and want to save the "Vote" with fields like: MongoDate, option, voter name, and maybe couple more fields.

我计划在一个给定的问题上获得数千甚至数百万的无限制投票".

I am planning to have unlimited "Votes" in the thousands and even in millions on a given question.

在检索数据方面:我希望能够主要按日期查询并显示在图表中,例如每小时、每天、每月...间隔的股票价格换句话说,它就像时间序列.我不确定 MongoDB 中文档的格式";

In terms of retrieving the data : I would like to be able to query it mainly by Date and present in charts, like a stock price with hourly, daily, monthly... intervals In other words it is like time series. I am not sure on the "format" of the document in MongoDB;

推荐答案

一种合理的方法是拥有一个投票集合,其中每个文档看起来像:

One reasonable way to do it would be to have a votes collection, where each document looks like:

<代码>{v: 'a',//投票给第一个选项
d: Date(),//日期
n: '鲍勃',
...
}

然后,在日期字段上建立索引.但是,如果您必须最终将其分片,请注意不要单独对日期字段进行分片.我将字段名称列为单个字符,因为每个字段的名称都存储在 mongoDB 中,因此为了更好的空间效率,您应该使用较短的名称.如果您不关心空间,则使用更长、信息量更大的名称可能也不错.

Then, index on the date field. Be careful not to shard on the date field alone, though, if you have to end up sharding this. I listed the field names as single characters because the name of every field is stored in mongoDB, so for better space efficiency, you should use shorter names. If you aren't concerned about space, a longer, more informative name is probably fine.

这篇关于用于存储“投票"的高效文档格式在 Mongo DB 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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