Loopback does not support group by or distinct query. But we can achieve in loopback another way. Let see a solution for this issue.
1. Distinct
// Let User is a Model and we want to get unique names var collection = User.getDataSource().connector.collection(<Model_Name>); collection.distinct( "name", function(err, records) { if(err) { return cb(err); } else { return cb(null, records); } });
distinct query with condition.
// Let User is a Model and we want to get unique names var collection = User.getDataSource().connector.collection(<Model_Name>); collection.distinct( "name", {name: {$ne:'abc'}} function(err, records) { if(err) { return cb(err); } else { return cb(null, records); } });
2. Using aggregation
var collection = User.getDataSource().connector.collection(<Model_Name>); collection.aggregate( [ { $match: { gender: 'female' } }, { $group: { _id: $name} }, { $project: { name:$_id, _id:0 } } ], function(err, records) { if(err) { return cb(err); } else { return cb(null, records); } });