Express 有没有办法在pre-find-hook中设置参数?
参考:“产品”, localField:“\u id”, foreignField:“主要产品” }); 有没有办法在pre-find hook中设置一个参数,这样我就可以只找到具有Express 有没有办法在pre-find-hook中设置参数?,express,mongoose,Express,Mongoose,参考:“产品”, localField:“\u id”, foreignField:“主要产品” }); 有没有办法在pre-find hook中设置一个参数,这样我就可以只找到具有productType=“main”的产品。这是因为我正在填充每个mainProduct的变体,所以不想重复对变体的查询 const productSchema = new Schema( { name: { type: String, required: true }, category: { ty
productType=“main
”的产品。这是因为我正在填充每个mainProduct的变体,所以不想重复对变体的查询
const productSchema = new Schema(
{
name: { type: String, required: true },
category: { type: Schema.Types.ObjectId, ref: 'Category'},
user: { type: Schema.Types.ObjectId, ref: "User" },
description: { type: String, required: true },
mainProduct: { type: Schema.Types.ObjectId, ref: 'Product'},
productType: {
type: String,
enum: productTypes,
default: "main",
},
},
{
timestamps: true,
toJSON: { virtuals: true }
}
);
productSchema .pre("find", function (next) {
//only find the product which have productype='main'
this.populate({ path: 'category' }).populate({ path: 'user' }).populate('variants');
next();
});
productSchema.virtual('variants', {