【发布时间】:2020-04-18 12:38:51
【问题描述】:
我正在使用 2 个模型:
产品和事件。
产品模型有来自事件 (_id) 的引用 Event 模型有来自 Product (_id) 的引用。
到目前为止,我发现如果我想更新一个事件并添加或退出产品,我还必须使用另一个操作来更新产品模型。删除也是如此(如果我删除一个事件,我必须更新与该事件关联的产品并将它们删除)。
我的问题是,例如: 我有一个具有 event_id:[1,2,3,4}] 的产品(id 1) 我有一个具有 product_id: [1,2] 的事件 (id 1)。
如果我更新事件 (id 1) 并取出产品 1,则现在的事件将只有产品 id 2,但产品仍然有事件 id 1。
为此,我正在使用钩子:特别是为了更新我正在这样做:
EventSchema.post("findOneAndUpdate", function(doc) {
Product.updateMany({
_id: {
$in: doc.product
}
}, {
event: doc._id.toString()
}, {
multi: true
}, function(error, product) {
if (error) console.log(error)
})
})
如何更新 Product 模型,取出 event_id 列数组中的 id?我希望我的解释清楚。
事件 SHCEMA
const EventSchema = new Schema({
client: {
type: [{
type: Schema.Types.ObjectId,
ref: 'Client'
}]
},
product: {
type: [{
type: Schema.Types.ObjectId,
ref: 'Product'
}]
},
date: {
type: Date,
maxlength: 64,
lowercase: true,
trim: true
},
place: {
type: String,
maxlength: 1200,
minlength: 1,
},
price: {
type: Number
},
comment: {
type: String,
maxlength: 12000,
minlength: 1,
},
},
{
toObject: { virtuals: true },
toJSON: { virtuals: true }
},
{
timestamps: true
},
);
客户端架构
const ClientSchema = new Schema(
{
first_name: {
type: String,
maxlength: 64,
minlength: 1,
required: [true, "Client first name is required"]
},
last_name: {
type: String,
maxlength: 64,
minlength: 1
},
address: {
type: String,
maxlength: 1200,
minlength: 1
},
phone: {
type: String,
maxlength: 64,
minlength: 1
},
email: {
type: String,
maxlength: 64,
lowercase: true,
trim: true
},
web: {
type: String,
maxlength: 1200,
minlength: 1
},
comment: {
type: String,
maxlength: 12000,
minlength: 1
},
event: {
type: [
{
type: Schema.Types.ObjectId,
ref: "Event"
}
]
}
},
{
toObject: { virtuals: true },
toJSON: { virtuals: true }
},
{
timestamps: true
}
);
产品架构:
const ProductSchema = new Schema(
{
name: {
type: String,
maxlength: 64,
minlength: 1,
required: [true, "Product name is required"]
},
description: {
type: String,
maxlength: 12000,
minlength: 1
},
comment: {
type: String,
maxlength: 12000,
minlength: 1
},
state: {
type: String,
maxlength: 64,
minlength: 0
},
available: {
type: Boolean,
default: true
},
price: {
type: Number
},
category: {
type: [
{
type: Schema.Types.ObjectId,
ref: "Category"
}
]
},
event: {
type: [
{
type: Schema.Types.ObjectId,
ref: "Event"
}
]
},
image: {
type: [
{
type: Schema.Types.ObjectId,
ref: "Image"
}
]
}
},
{
toObject: { virtuals: true },
toJSON: { virtuals: true }
},
{
timestamps: true
}
);
输入数据:product/:id route
{
"available": false,
"category": [
"5e04cd609b1c6812c8f9a9d1"
],
"event": [
"5e07f73d72503f6659f40b26"
],
"image": [
"5e05f9dd66432544b7bf0221"
],
"_id": "5e05f9d166432544b7bf0220",
"name": "Mesa ratona",
"price": 1200,
"comment": "-",
"description": "-",
"__v": 0,
"modelName": "Product",
"id": "5e05f9d166432544b7bf0220"
}
输入数据事件/:id
{
"client": [
"5e05f743cd57804386a92a89"
],
"product": [
"5e05f9d166432544b7bf0220",
"5e06464b811a954e831eafcf",
"5e064c5c811a954e831eafd3"
],
"_id": "5e07f73d72503f6659f40b26",
"date": "2020-01-12T00:45:33.069Z",
"place": "EVENT 1",
"price": 123,
"comment": "EVENT 1",
"__v": 0,
"id": "5e07f73d72503f6659f40b26"
}
输入数据客户端/:id
{
"event": [
"5e07f73d72503f6659f40b26"
],
"_id": "5e05f743cd57804386a92a89",
"first_name": "Ernesto",
"last_name": "De Lucía",
"address": "Calle Santa Fé 3129 Piso 5 Depto \"B\" Capital Federal",
"email": "ernestodl@gmail.com",
"phone": "+54 011 1567432984",
"web": "-",
"comment": "-",
"__v": 0,
"full_name": "Ernesto De Lucía",
"id": "5e05f743cd57804386a92a89"
}
谢谢!
【问题讨论】:
-
您可以添加产品和事件架构代码以及示例输入文档吗?
-
对不起,输入数据是什么意思?
-
json格式的示例文档
-
顺便说一下,我在一些地方将 min 属性更正为 minlength,例如在 client.web 字段中。
-
完美!谢谢。我已经添加了一些输入数据
标签: node.js express mongoose mongoose-schema