Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 猫鼬填充多个ID_Node.js_Mongodb_Mongoose_Population - Fatal编程技术网

Node.js 猫鼬填充多个ID

Node.js 猫鼬填充多个ID,node.js,mongodb,mongoose,population,Node.js,Mongodb,Mongoose,Population,我有这些模式 var DriverSchema = Schema({ name: String, groups: { type: String, ref: 'DriverGroup' } }); var DriverGroupSchema = Schema({ name: String }); module.exports = mongoose.model('DriverGroup', DriverGroupSchema); module.exports

我有这些模式

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);
我有以下驱动程序组:

[{
  id: '12345',
  name: 'Group1'
}, {
  id: '6789',
  name: 'Group2'
}]
这位司机:

{
  name: 'Driver1',
  groups: '12345,6789'
}
我怎样才能按人口得到这些群体?我用的是:

Driver.find().sort('name').populate('groups')
但无法正常工作。

如果要以这种方式填充组,则存储组不正确。 无法分析字符串以确定单独的实体

它应该是一个数组

groups: '12345,6789'
应该是

groups: ['12345,6789']
此外,通过查找_idnot name字段来填充工作

因此,需要更改您的模式:

var DriverSchema = Schema({
  name: String,
  groups: [{ type: string, ref: 'DriverGroup' }]
});
在您的案例中,您需要将_idtoo set _id设置为与name相同

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