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

Advertisements

2 thoughts on “Meteor – Distinct MongoDB Query”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s