mongodb - Meteor Publish Distinct Values of Field in Collection -
i'm stuck on pretty simple scenario in meteor:
i have huge collection of things many fields, of them containing quite bit of text.
i want create page searching collection.
one of fields each item in collection has "category".
i'd give user ability filter category.
for that, need publish distinct values of category field in collection.
i can't figure out way without publishing whole collection takes way long. how can publish distinct categories , use them fill dropdown?
bonus question , related: how publish count of items in collection without publishing whole collection?
a starting point make easier normalize categories
separate database collection.
however assuming not possible or practical, best (though imperfect) solution publish 2 separate versions of collection, 1 returns categories
field of entire collection , returns fields of collection selected category only. following:
// server meteor.startup(function(){ meteor.publish('allthings', function() { // return id , categories field things return things.find({}, {fields: {categories: 1}}); }); meteor.publish('thingsbycategory', function(category) { // return fields things having selected category // can subscribe via client-side session variable // e.g., meteor.subscribe("thingsbycategory", session.get("category")); return things.find({category: category}); }); });
note still need assemble array of categories client side things cursor (for example, using underscore's _.pluck , _.uniq methods grab categories , remove dups). data set smaller working single-field documents now.
(note ideally, want use mongo's distinct() method in publish function publish distinct categories, not possible directly returns array cannot published).
Comments
Post a Comment