mongoid_taggable with aggregation for Mongoid 5

23 March 2017 • Tags: mongoid — gem

Turned out that my migration earlier this week ran into a few bumps with mongoid_taggable. The big thing the previous used version from colibri-software used was the aggregation framework which in the case of generating the tags index is far superior to the map_reduce of the older versions. The mongoid 5 compatible version I picked as a replacement didn’t use the aggregation framework, caused me headache and I decided to upgrade colibri’s version. Nothing special there, just remember that you need to append a .to_a if you pass a $out stage to aggregate().

  {"$unwind" => "$tags_array"},
  {"$group" => {"_id" => "$tags_array}},
  {"$project" => {"tags_array: 1}},
  {"$out" => "things_tags"}
]).to_a  # don't rmeove the to_a or the things_tags collection will be empty