【发布时间】:2021-05-11 23:44:32
【问题描述】:
有 2 个模型。
- 用户
- 植物
最终用户应该能够添加多个数量的同一植物。 我的用户模型是:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
email: {
type: String,
required: true,
},
cart: [{
type: Schema.Types.ObjectId,
ref: 'Plants',
}
],
address: [{
type: Schema.Types.ObjectId,
ref: 'Contacts'
}]
});
module.exports = mongoose.model('Users', userSchema)
我的植物模型是
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const plantSchema = new Schema({
name: {
type: String,
required: true,
},
imageUrl: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
price: {
type: Number,
required: true
},
category: {
type: String,
required: true,
}
});
module.exports = mongoose.model('Plants', plantSchema)
希望我的用户包含多个重复的植物子文档。
0:6013ab17e5d05b3457cf983a
1:6017ce139e9f3d6d3a616f2b
2:6017ce139e9f3d6d3a616f2b
3:6017ce139e9f3d6d3a616f2b
4:6017ce139e9f3d6d3a616f2b
5:6017ce139e9f3d6d3a616f2b
我应该如何管理这个
【问题讨论】:
标签: javascript node.js reactjs mongodb mongoose