我需要将另一个集合中的字段设置为我的mongoose模式的外键。
我有一个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 }
});发布于 2019-10-01 14:30:01
MongoDB不是关系数据库,所以它不支持像外键这样的概念。在mongoDB中,您必须以一种可以轻松获取信息的方式组织数据。
您应该看看是否可以使用聚合或其他东西,但mongoDB没有外键。更重要的是,你应该在创建数据库时做出正确的选择,你应该根据自己的需求选择合适的数据库。
https://stackoverflow.com/questions/58178852
复制相似问题