[英]Mongoose: populate an object inside an array
我有一個這樣導出的模式:
const PackageSchema = new Schema({
name: { type: String, required: true },
maneuver: [
{
maneuverId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: ManeuverMainly,
},
period: { type: String, enum: ["day", "night"], required: true },
},
],
timestamp: { type: Date, default: Date.now() },
});
當我像這樣進行find()
時:
Package.find().populate("maneuver", "name").exec((err, data) => {
if (err) {
res.status(500).send({ message: "Failed!" });
return;
}
res.status(200).send(data);
});
我populate
方法不起作用。 如何使用ManeuverMainlySchema
中的name
列填充PackageSchema
中的每個maneuverId
ID?
Obs:我的ManeuverMainlySchema
如下:
const ManeuverMainlySchema = new Schema({
name: { type: String, required: true },
description: { type: String, required: true },
timestamp: { type: Date, default: Date().now },
});
取自ZCCADCDEDB567ABAE643E15DCF0974E503Z 填充包含 ref 的對象數組,您必須在要填充的數組的 object 中指定字段。
Package.find().populate("maneuver.maneuverId", "name").exec((err, data) => {
if (err) {
res.status(500).send({ message: "Failed!" });
return;
}
res.status(200).send(data);
});
Package.find().populate(["maneuver.maneuverId", "name"]).exec((err, data) => {
if (err) {
res.status(500).send({ message: "Failed!" });
return;
}
res.status(200).send(data);
});
如果要填充其中之一,則不需要在填充中使用數組作為填充(“maneuver.maneuverId”)或填充(“名稱”)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.