我有一个这样导出的schema:
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
?
Obs:my ManeuverMainlySchema
bellow:
const ManeuverMainlySchema = new Schema({
name: { type: String, required: true },
description: { type: String, required: true },
timestamp: { type: Date, default: Date().now },
});
型
2条答案
按热度按时间vulvrdjw1#
从Mongoose populate with array of objects containing ref中提取,你必须指定数组对象中的字段。
字符串
niwlg2el2#
字符串
如果你想
populate
只有一个这样的:populate("maneuver.maneuverId")
,populate("name")