Mahahari
Mahahari

Reputation: 1011

Distinct Query is Sails js

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.

Upvotes: 0

Views: 2176

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151132

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.

Upvotes: 1

Related Questions