1

有人可以帮我解决这个模式的人口吗?我需要通过他们的 userId 填充员工数组。

var PlaceSchema = new Schema ({
    name:       { type: String, required: true, trim: true },
    permalink:  { type: String },
    country:    { type: String, required: true },
         ...long story :D...
    staff:      [staffSchema],
    admins:     [adminSchema],
    masterPlace:{ type: Boolean },
    images:     []

});

var staffSchema = new Schema ({
    userId: { type: Schema.Types.ObjectId, ref: 'Account' },
    role: { type: Number }
});

var adminSchema = new Schema ({
    userId: { type: Schema.Types.ObjectId, ref: 'Account'}
})

var Places = mongoose.model('Places', PlaceSchema);

我尝试使用此查询,但没有成功。

Places.findOne({'_id' : placeId}).populate('staff.userId').exec(function(err, doc){
    console.log(doc);
});
4

1 回答 1

0

填充旨在作为一种从集合中的相关模型中“提取”信息的方法。因此,与其“直接”指定相关字段,不如引用相关字段,以便文档看起来将所有这些子文档都嵌入到响应中:

Places.findOne({'_id' : placeId}).populate('staff','_id')
    .exec(function(err, doc){
    console.log(doc);
});

第二个参数只返回您想要的字段。所以它“过滤”响应。

文档中有关于填充的更多信息。

于 2014-03-14T01:29:34.073 回答