如何在猫鼬中设置外键。?
我需要将另一个集合中的字段设置为我的猫鼬模式的外键。
我对“ productSchema”有一个计数器“ pid”。我需要将“ pid”设置为“ orderSchema”的外键。
const orderSchema = mongoose.Schema({
id: mongoose.Schema.Types.ObjectId,
product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product',
required:
true },
quantity: { type: Number, default: 1 }
});
const productSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true },
price: { type: Number, required: true },
productImage: { type: String }
});
我需要将另一个集合中的字段设置为我的猫鼬模式的外键。我对“ productSchema”有一个计数器“ pid”。我需要将“ pid”设置为“ orderSchema” const的外键...
回答如下:Well MongoDB不是关系数据库,因此它不支持外键之类的概念。在mongoDB中,您必须以一种可以轻松获取信息的方式来构造数据。