【发布时间】:2021-12-12 11:08:54
【问题描述】:
我正在构建模式 (Meal),我希望从不同模式 (Meat) 的不同字段 (meatName) 中获取此模式中的字段 (meat_name) 之一。 我知道填充方法,但它引用整个集合,我想引用特定字段。
-Meat Schema-
const meatSchema= new Schema({
MeatName: String,
MeatDescription: String,
});
module.exports = mongoose.model("Meat", meatSchema);
-Meal Schema-
const mealSchema= new Schema({
mealName: String,
mealPrice: Number,
meat_name: {
type: Schema.Types.ObjectId,
ref: "Meat" /*populate method return the entire collection, but I want just the meatName field in that collection */,
},
});
module.exports = mongoose.model("Meal", mealSchema);
【问题讨论】:
标签: mongodb mongoose mongoose-schema mongoose-populate