bring2dip
bring2dip

Reputation: 885

Distinct query not working in Mongoose

I have a model as follow:

var GamePlayerSchema = new Schema({  
 game: {
    type: Schema.ObjectId,
    ref: 'Game',
    required:true
},
group: {
    type: Schema.ObjectId,
    ref: 'Group'
},
user: {
    type: Schema.ObjectId,
    ref: 'User'
},
paymentDone: {
    type: Boolean,
    default:false
}});

In the above model any player can play multiple games.Show when I query the collection user with same name are shown twice if they are registered in two games. To find distinct players I have used the query as below:

GamePlayer.find().distinct('user',function(err, results){

});

But this is not giving the distinct results. How do I achieve distinct result in mongoose.

Upvotes: 0

Views: 4184

Answers (1)

Sede
Sede

Reputation: 61225

distinct is a collection method change your query to the following:

GamePlayer.distinct('user', function(err, results){

});

Upvotes: 3

Related Questions