我目前有两个模式。
var carSchema = new mongoose.Schema({
id: {
type: Number,
unique: true,
required: true
},
make: {
type: String,
required: true
},
model: {
type: String,
required: true
},
year: {
type: Number,
required: true,
min: config.MIN_YEAR,
max: config.MAX_YEAR
},
color: {
type: String
},
price: [
// don't know what to put here...
],
});
var priceSchema = new mongoose.Schema({
car_id: {
type: Number
},
amount: {
type: Number,
min: 0
},
year: {
type: Number,
min: config.MIN_YEAR,
max: config.MAX_YEAR
}
});
正如您从上面的两个模式中看到的那样,我试图从汽车模式中引用价格模式。我知道您可以通过引用 ObjectId (_id
) 来实现,但我需要通过 car_id
来引用价格架构。原因是因为我正在从一个已经定义了表头的 CSV 文件中读取所有数据。 mongoose 中是否有任何可能的方法来进行此引用?
最佳答案
在汽车模式之上定义价格模式,然后将价格模式放入您的汽车模式。
...
color: {
type: String
},
price: [priceSchema]
...
关于javascript - Mongoose/MongoDB - 引用另一个没有 ObjectId 的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44870243/