Efficient document format to store “Votes” in Mong

2019-08-21 21:18发布

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.

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;

1条回答
唯我独甜
2楼-- · 2019-08-21 21:42

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

{ v: 'a', //voted for the first option
d: Date(), //the date
n: 'Bob',
...
}

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.

查看更多
登录 后发表回答