Matthew Slaton
Matthew Slaton

Reputation: 349

ReferenceError: db is not defined while trying to find distinct entries in database

I am getting db is not defined when trying to use mongodb's distinct in meteor.

  Template.displayinbox.helpers({
     inboxlistings: function() {
       itemscount = db.Messages.distinct( "fromUsername" ).count;
       return db.Messages.distinct( "fromUsername" );
   }
  });

I want to be able to return only distinct documents in my collection from the username field and count all those documents that is posted by the fromUsername. How would I go about doing this in Meteor?

Upvotes: 0

Views: 1301

Answers (1)

Faysal Ahmed
Faysal Ahmed

Reputation: 1542

When you're querying anything in the Meteor code itself, you don't need to write db first. You have to use the variable name that used to instantiate the Mongo Object. Let's say you defined your Mongo db like this.

example = new Mongo.Collection('Messages');

then within your helper you just use the typical query using this object.

Template.displayinbox.helpers({
inboxlistings: function() {
var items = example.find();
return _uniq(items,function(i){return i.fromUserName;});
}
});

Upvotes: 1

Related Questions