开发者

What's the most efficient way of incrementing weekly totals in mongo?

In a scenario where I have weekly total points as an array of integers per user, I was reading in tip #7 in the "50 tips and tricks for MongoDB developers" that for best performance in Mongo that I should pre-populate the array. Thus for each user I should say insert 20 weeks ahead of time. e.g.

Profile:{name:bob, totals:[{week:1,pts:0}, {week:2,pts:0}, {week:2,pts:0}, {week:2,pts:0}...etc..]}

If that's the case, whats the best way to update the points field? I can do a $inc command if the entry for the week exists, but should I do an addToSet command first to make sure that the entry for the week exists and then do the $inc command? That seems inefficient... will the $inc command be combined with an upsert of some kind?

The book's example suggests that instead of embedding totals in the profile, I can say create an annual totals document for each user and have an array of weekly points total in that document, pre-populating 52 weeks worth. I could then run a batch job to create a new ann开发者_如何学运维ual totals document for each user if they are on their last week.

Is it better to store my totals in a seperate collection rather than embed it in the profile with my use case?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜