Necesita contar etiquetas distintas en todos los registros en una consulta mongodb , usando JS .
Estructura de registro:
{ "_id": { "$oid": "62e593aed8fd9808777225e8" }, "title": "“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”", "author": { "name": "Albert Einstein", "url": "https://quotes.toscrape.com/author/Albert-Einstein" }, "tag": [ "change", "deep-thoughts", "thinking", "world" ] }Esto podría ser útil. Además de obtener los diferentes valores para el campo, devuelve el número de apariciones:
db.collection.aggregate([ { "$unwind": "$tag" }, { "$group": { "_id": "$tag", "total": { "$sum": 1 } } }, ])Puedes probar aquí: https://mongoplayground.net/p/yXLYkJKO3Wf