学习RoR和Mongo,为什么不统计数组求票数
我对 Mongo 和 RoR 完全陌生,有 PHP 背景。
我刚刚浏览了有关数据建模的教程 http://www.mongodb.org/display/DOCS/ MongoDB+Data+Modeling+and+Rails
并被一个问题所震惊:为什么教程会建议将投票存储在一个字段中并将该字段更新为
db.stories.update({_id: story_id, voters: {'$ne': user_id}}, {'$inc': {votes: 1}, '$push': {voters: user_id}});instead of just
db.stories.update({_id: story_id, voters: {'$ne': user_id}}, {'$push': {voters: user_id}});
然后数然后
Story.voters.countto get the count of the number of users who have voted?
我知道这是一个教程,但它似乎不是管理数据的最有效方法。
I'm completely new to Mongo and RoR, coming from a PHP background.
I was just going through this tutorial about data modelling
http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails
and was struck with the question of why the tutorial would recommend storing the votes in a field and updating that field as
db.stories.update({_id: story_id, voters: {'$ne': user_id}}, {'$inc': {votes: 1}, '$push': {voters: user_id}});
instead of just
db.stories.update({_id: story_id, voters: {'$ne': user_id}}, {'$push': {voters: user_id}});
and then counting then
Story.voters.count
to get the count of the number of users who have voted?
I know it's a tutorial, but it doesn't seem like the most efficient way to manage the data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
本教程可以通过解释维护计数缓存字段(投票)的目的来做得更好,因为只有当您打算对数组大小进行范围查询时才需要它。
来自文档:
将来,随着查询功能的不断增长,我们可能不必这样做。请参阅 JIRA 票证 SERVER-478。
The tutorial could do a better job by explaining the purpose of maintaining the count cache field (votes), since it's only needed if you intend to do range queries on the array's size.
From the docs:
In the future we probably won't have to do that as the query capabilities continue to grow. See JIRA ticket SERVER-478.