问题描述
我是 Node JS 和 MongoDB 的新手,所以我尝试构建一些项目,但遇到了问题。希望有人能够帮助我。
所以我要做的是
-
创建产品
-
创建插件并用产品填充它
我在 Total 中成功的是
- 产品信息(我成功地做到了这一点,因为在 req.params 我发送了产品 ID)
我现在需要在 Total 上获取来自特定产品的插件,但我无法做到这一点,因为 id 位于 addons.product 而不是 addons._id。
产品架构:
const product = new mongoose.Schema({
name: {
type:String
},description:{
type:String
}
})
插件架构
const addonsSchema = new mongoose.Schema({
addonsName:{
type:String
},product:{
type: mongoose.Schema.Types.ObjectId,ref: 'product'
},})
addonsSchema.pre(/^find/,function(next) {
this.populate({
path: 'product',select: '-__v'
});
next();
});
totalSchema
const totalSchema = new mongoose.Schema({
name:{
type:String
},total:[{
product:{
type: mongoose.Schema.Types.ObjectId,ref: 'product'
},addons:{
type: mongoose.Schema.Types.ObjectId,ref: 'addons'
}
}],dateCreated:{
type:String
}
})
totalSchema.pre(/^find/,function(next) {
this.populate({
path: 'addons.product',select: '-__v'
});
next();
});
totalSchema.pre(/^find/,function(next) {
this.populate({
path: 'addons.addons',select: '-__v'
});
next();
});
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)