I have a MongoDB schema for blog posts that looks like:
{
title: String,
date: Date,
description: String,
tags: [String]
}
So, for example, a given blog post might have a document that looks like:
{
title: 'My blog post about MongoDB',
date: '2020-07-06T07:00:00.000+00:00',
description: 'A blog about Mongo',
tags: ['MongoDB', 'Javascript']
}
I'm trying to implement a query across my db that returns a mapping of each tag and a count of how many times that tag appears in the collection. So, for example if my collection has three posts (A, B, and C), where:
A.tags = ['MongoDB', 'Random']
B.tags = ['MongoDB', 'React']
C.tags = ['Nature', 'Random']
The query would return:
[
{ 'MongoDB', 2 },
{ 'Random', 2 },
{ 'React', 1 },
{ 'Nature', 1 },
{ 'Coding', 1 },
]
Is there a way to do so using some sort of aggregation pipeline or something similar?
Thanks!