Meteor – Distinct MongoDB Query

Meteor doesn’t support distinct in query. Say, i have a dataset as follow.

{
  "student":
  [
    { "_id": "1", "class": "1A", "name": "Peter" },
    { "_id": "2", "class": "1A", "name": "Jack" },
    { "_id": "3", "class": "1B", "name": "Mary" },
    { "_id": "4", "class": "1C", "name": "Sam" },
    { "_id": "5", "class": "1C", "name": "Ann" },
  ]
}

 

What i would like to know is how many distinct classes are there. Here is the solution suggested by Jonathan Pidgeon from Empire5Design.

function getDistinctClasses() {
  var data = Student.find().fetch();
  var distinctData = _.uniq(data, false, function(d) {return d.class});
  return _.pluck(distinctData, "class");
}

 

Done =)

Reference: coderwall – Get unique values from a collection in Meteor

2 thoughts on “Meteor – Distinct MongoDB Query”

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.