MongooseJS: .populate()不返回父模型的字段



目前有产品和图书两个型号;其中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模型中的字段(不包括Product模型中的字段)。

newCart = new Cart({...}) 
newCart.save()
let populatedCart = await newCart.populate({ path: "products.productID", model: Product})

您的问题可能是Mongoose的鉴别器功能的结果。当您使用discriminator方法生成继承现有模型的新模型时,Mongoose将为子模型构建一个新集合,并将其文档存储在该集合中。填充时查询的唯一集合是在Cart实例上运行,它只填充Book模型中的字段。解决这个问题的一个潜在方法是显式地将父模型中的字段包含在子模型的模式中。通过这种方式,当在Cart实例上执行填充操作时,将包括来自Product模型的字段。另一种替代方法是使用选择选项执行填充方法,以包含来自父模型的所需字段。下面是一个例子:

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);

最新更新