[英]MongooseJS: .populate() does not return fields from the parent model
目前有 2 个模型产品和书籍; Book 继承自 Product,如下所示:
const ProductSchema = new mongoose.Schema(
{
name: {...},
description: {...},
images: [{... }],
inventory: { ... },
department: { ... },
....
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
discriminatorKey: "kind",
}
)
Model = mongoose.model("Product", productSchema)
const BookSchema = new mongoose.Schema({
subtitle: { ... },
abstract: { ... },
publisher: { ... },
authors: { ... },
...
},
{
timestamps: true, discriminatorKey: "kind",
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
)
Book = Product.discriminator("Book", BookSchema)
此外,还有一个 Cart 模式,它有一个子文档“products”,其中包含一个引用字段“bookID”,如下所示:
const cartItem = new mongoose.Schema({
productID: {
type: mongoose.Types.ObjectId,
ref: "Product",
required: [true, "Please provide productID. "]
},
quantity: { ... },
sessionID: { ... },
})
const cartSchema = new mongoose.Schema({
products: [cartItem],
active: {
type: Boolean,
default: true,
hide: true,
},
sessionID: {
type: mongoose.Types.ObjectId,
ref: "Session"
}
}, {
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
})
Cart = mongoose.model("Cart", cartSchema)
我正在使用 mongoosejs v-6.8.3
问题是 Cart 实例上的 .populate() 仅返回 Book model 中的字段(不包括 Product 模型中的字段)。
newCart = new Cart({...})
newCart.save()
let populatedCart = await newCart.populate({ path: "products.productID", model: Product})
您的问题可能是 Mongoose 的鉴别器功能如何运作的结果。 Mongoose 将为子 model 构建一个新集合,并在您使用鉴别器方法生成新的 model 时将其文档存储在该集合中,该新 model 继承现有的 model。填充时查询的唯一集合在 Cart 实例上运行,它只填充 Book model 中的字段。此问题的一个潜在解决方法是在子模型的架构中显式包含父 model 中的字段。 以这种方式,当对 Cart 实例执行填充时,将包含来自 Product model 的字段。 另一种方法是执行 fill 方法,并选择选择选项以包括父 model 中的所需字段。这是一个示例:
const ProductSchema = new mongoose.Schema({
name: {...},
description: {...},
images: [{... }],
inventory: { ... },
department: { ... },
....
}, {
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
discriminatorKey: "kind"
});
const Model = mongoose.model("Product", productSchema);
const BookSchema = new mongoose.Schema({
subtitle: { ... },
abstract: { ... },
publisher: { ... },
authors: { ... },
...
}, {
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
discriminatorKey: "kind",
});
const Book = Model.discriminator("Book", BookSchema);
const cartItem = new mongoose.Schema({
productID: {
type: mongoose.Types.ObjectId,
ref: "Product",
required: [true, "Please provide productID. "]
},
quantity: { ... },
sessionID: { ... },
});
const cartSchema = new mongoose.Schema({
products: [cartItem],
active: {
type: Boolean,
default: true,
},
sessionID: {
type: mongoose.Types.ObjectId,
ref: "Session"
}
}, {
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
});
const Cart = mongoose.model("Cart", cartSchema);
let newCart = new Cart({...});
await newCart.save();
let populatedCart = await Cart.findById(newCart._id).populate({ path: "products.productID", model: "Product", select: '-__v' });
console.log(populatedCart);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.