0

I want to retrieve distinct records from mongodb .I tried the below method,

        Model.find()
             .distinct('username')
             .where({ age: 21 })
             .exec(function(err, user) { });

But iam getting error as below.

     TypeError: Object [object Object] has no method 'distinct'.

Please help.

Mahahari
  • 971
  • 1
  • 19
  • 36

1 Answers1

1

Those functions are not really what you want, and there is a better way to get at the "good stuff" in the model used by sails.

You really want the aggregate method for this, and as a sample, your equivalent would be:

   Model.native(function(err,collection){
       collection.aggregate([
           { "$match": { "age": 21 } },
           { "$group": { "_id": '$username' } }
       ],function(err,docs) {
          // something here

       });
   });

For more under the hood parts, check out this answer for some more links on the Aggregation framework.

Community
  • 1
  • 1
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317