Friday, 12 May 2017

Mongoose populate multiples ids

I have those schemas

var DriverSchema = Schema({
  name: String,
  groups: {
     type: String,
     ref: 'DriverGroup'
  }
});


var DriverGroupSchema = Schema({
  name: String
});

module.exports = mongoose.model('DriverGroup', DriverGroupSchema);
module.exports = mongoose.model('Driver', DriverSchema);

I have those driver groups:

[{
  id: '12345',
  name: 'Group1'
}, {
  id: '6789',
  name: 'Group2'
}]

and this driver:

{
  name: 'Driver1',
  groups: '12345,6789'
}

How I can get the groups by population? I am using this:

Driver.find().sort('name').populate('groups')

But does not works properly.



via jfergt

No comments:

Post a Comment