这是我拥有的,它的工作原理:
var comboSchema = new Schema({
components: [{
type: Schema.Types.ObjectId,
ref: "Component"
}]
})
这就是我想要实现的目标:
var comboSchema = new Schema({
components: [{
type: Schema.Types.ObjectId,
ref: "Component",
amount: {type: Integer}
}]
})
是否有可能在MongoDB中,如果不是最好的解决方法是什么?
谢谢 :-)
由于提供了元素或字段名称,因此该模式起作用
var comboSchema = new Schema({
components: [{
type: Schema.Types.ObjectId,
ref: "Component"
}]
})
现在你想要创建一个单一的错误,你想在两个不同的字段的对象中创建没有名称的模式名称
正确创建这样的模式的方法是在数组中包含其他包含类型的变量
var comboSchema = new Schema({
components: [{
id: {
type: Schema.Types.ObjectId,
ref: "Component"
},
amount: { //If you want to make every component amount
type: Number
}
}]
})
要么
var comboSchema = new Schema({
amount: { type: Number },
//If you want to make only single amount on multiple components
components: [{
componentId: {
type: Schema.Types.ObjectId,
ref: "Component"
}
}]
})
但在这两种情况下你都无法直接填充。您需要使用聚合来获取嵌入文档的数据。