Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to collect all tags and count these in Meteor

Tags:

mongodb

meteor

i need exactly this map/reduce function in meteor: http://cookbook.mongodb.org/patterns/count_tags/

Read all tags from all entries and give back a list of unique tags with the amount in the collection

How would I implement this. I'm using standalone Mongodb. Collection Layout:

[_id] => 1234
[headline] => My First Post
[body] => Alot of stuff
[isPrivat] => 
[tags] => Array (
    [0] => test
    [1] => php
    [2] => perl
)

I found this question/answer, but I can't get this to do what I wan't :
Does Meteor have a distinct query for collections?

What is the most elegant way to do this. Thank you for your time

like image 764
Johannes Avatar asked Dec 21 '22 10:12

Johannes


1 Answers

At the current moment, the most elegant solution would be aggregation framework:

db.items.aggregate(
    { $unwind : "$tags" },
    { $group : {
        _id : "$tags",
        count: { $sum : 1 }
    }}
);

It will output result like this one:

{ "result" : [ 
    { "_id" : "perl",
      "count" : 2 }, 
    { "_id" : "php",
      "count" : 1 }, 
    { "_id" : "test",
      "count" : 1 }],
  "ok" : 1 }

This is mongodb shell syntax. I not familiar with meteor, so I'll not try to write some code. Hope this helps!

like image 84
Andrew Orsich Avatar answered Jan 15 '23 05:01

Andrew Orsich