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
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With