用于存储“投票"的高效文档格式在 Mongo DB 中?

编程入门 行业动态 更新时间:2024-10-26 06:38:55
本文介绍了用于存储“投票"的高效文档格式在 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 中?

本文发布于:2023-11-22 21:03:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619013.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:高效   文档格式   quot   Mongo   DB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!