I have a GridFS collection with image documents – each has a field metadata.tags
with an array of tags ['tag1', 'tag2']
.
How would I query and display a reactive list of tags used—without overlap? Thanks!
I have a GridFS collection with image documents – each has a field metadata.tags
with an array of tags ['tag1', 'tag2']
.
How would I query and display a reactive list of tags used—without overlap? Thanks!
There are several ways to do this, but here’s one really quick/easy way. Setup a subscription that queries for all of your image tags. On your client side when you retrieve the image tags filter them using Underscore’s uniq
and pluck
functions.
Other options include using Mongo’s distinct
method with Meteor’s Collection rawCollection
option, or using a third party package that gives you Mongo distinct capabilities like monbro:mongodb-mapreduce-aggregation.
Sounds like a good job for map-reduce if you don’t need completely fresh data all the time.
Other than that, I think it’d be very hard to do this efficiently. Sending all tags will consume a lot of memory and cpu and aggregation pipelines aren’t reactive.
If you do need it to be fresh and reactive, I’d suggest making a separate collection for tags, where each tag is a record (maybe with the count or something). But that means you need to maintain this collection as tags and images are changed/inserted/removed