I have a problem that i am not sure how to solve efficiently.
I have two collections:
1)
hits = {
'day': '',
'number_of_hits': 0
'user_id': 0
}
2)
stats = {
'day': '',
'total_number_of_hits': 0
'user_id': 0
...
some other stuff
}
I need to get sums of number_of_hits for each day (there can be many documents for each day containing
various number of hits) and update stats collections with those sums in quickest way possible.
This has to be done for each user_id found in hits collection
I can for example get an aggregates for hits collection and then in eg. loop update stats collection.
But something is telling me it’s not a good way.
Also sometimes stats collection may not have documents for some days, so they need to be created
instead of updated.
If you can get me any ideas it would be amazing 🙂
Thank you,
PabloX
May be try to re-design you structure as
and get only one document with all hits. You can calculate total at any time.