What is the best way to count all distinct values in mongodb ArrayField?
Here is my model:
class tweet(models.Model):
topic = models.JSONField()
3 sample documents:
1. ["investment", "economy"]
2. ["investment"]
3. ["economy", "politics"]
Desire result:
[{"investment":2},{"economy":2},{"politics":1}]
Maybe something like this:
tweet.objects.annotate(t=Func(F('topic'), function='$unwind')).values_list('t').annotate(c=Count('_id'))
any help would be appreciated
I have also posted on stackoverflow: