有没有办法在更新 MongoDb 之前从数据库获取数据 - Mongoose
Is there a way to get data from db before update for MongoDb - Mongoose
我想为我的项目做一个更新方法,但我无法解决这个问题。我的模型有一个现场调用 slug。如果我需要我添加数据以使这个值唯一。但是,我在我的更新功能上使用 findByIdAndUpdate
方法。我想知道有没有办法在更新这个模型之前获取数据?我是否必须向我的数据库发出至少 2 个不同的请求才能获取旧数据,或者我使用的这种方法是否让我有机会比较数据?因为如果标题字段已更改,我需要比较它并生成新的 slug 值。
类别模型
const mongoose = require('mongoose')
const CategorySchema = mongoose.Schema({
title: {
type: String,
required: true,
trim: true,
minLength: 3,
maxLength: 70
},
description: {
type: String,
requried: true,
trim: true,
minLength: 30
},
coverImage: {
type: String,
trim: true
},
slug: {
type: String,
unique: true,
required: true
}
}, {collection: "categories", timestamps: true})
module.exports = mongoose.model('category', CategorySchema);
更新函数
const update = async (req, res, next) => {
delete req.body.createdAt;
delete req.body.updatedAt;
try {
const data = req.body;
data.coverImage = req.file ? req.file.path.replace(/\/g, "/") : undefined;
data.slug = data.title ? slugCreator(data.title, null): undefined;
const result = await CategoryModel.findByIdAndUpdate(req.params.categoryId, data, { new: true, runValidators: true });
if (result) {
return res.json({
message: "Category has been updated",
data: result
});
}else{
throw createError(404, "Category not found.")
}
} catch (error) {
next(createError(error));
}
};
您可以先获取文档来解决您的问题,然后像下面的示例一样使用保存方法进行更新
const update = async (req, res, next) => {
delete req.body.createdAt;
delete req.body.updatedAt;
try {
const data = req.body;
//here you have the current category
const category = await CategoryModel.findById(req.params.categoryId);
if (!category) {
throw createError(404, 'Category not found.');
}
//do all you comparation and setting the data to the model...
category.slug = data.title ? slugCreator(data.title, null) : undefined;
category.coverImage = req.file
? req.file.path.replace(/\/g, '/')
: undefined;
await category.save();
return res.json({
message: 'Category has been updated',
data: category,
});
} catch (error) {
next(createError(error));
}
};
我想为我的项目做一个更新方法,但我无法解决这个问题。我的模型有一个现场调用 slug。如果我需要我添加数据以使这个值唯一。但是,我在我的更新功能上使用 findByIdAndUpdate
方法。我想知道有没有办法在更新这个模型之前获取数据?我是否必须向我的数据库发出至少 2 个不同的请求才能获取旧数据,或者我使用的这种方法是否让我有机会比较数据?因为如果标题字段已更改,我需要比较它并生成新的 slug 值。
类别模型
const mongoose = require('mongoose')
const CategorySchema = mongoose.Schema({
title: {
type: String,
required: true,
trim: true,
minLength: 3,
maxLength: 70
},
description: {
type: String,
requried: true,
trim: true,
minLength: 30
},
coverImage: {
type: String,
trim: true
},
slug: {
type: String,
unique: true,
required: true
}
}, {collection: "categories", timestamps: true})
module.exports = mongoose.model('category', CategorySchema);
更新函数
const update = async (req, res, next) => {
delete req.body.createdAt;
delete req.body.updatedAt;
try {
const data = req.body;
data.coverImage = req.file ? req.file.path.replace(/\/g, "/") : undefined;
data.slug = data.title ? slugCreator(data.title, null): undefined;
const result = await CategoryModel.findByIdAndUpdate(req.params.categoryId, data, { new: true, runValidators: true });
if (result) {
return res.json({
message: "Category has been updated",
data: result
});
}else{
throw createError(404, "Category not found.")
}
} catch (error) {
next(createError(error));
}
};
您可以先获取文档来解决您的问题,然后像下面的示例一样使用保存方法进行更新
const update = async (req, res, next) => {
delete req.body.createdAt;
delete req.body.updatedAt;
try {
const data = req.body;
//here you have the current category
const category = await CategoryModel.findById(req.params.categoryId);
if (!category) {
throw createError(404, 'Category not found.');
}
//do all you comparation and setting the data to the model...
category.slug = data.title ? slugCreator(data.title, null) : undefined;
category.coverImage = req.file
? req.file.path.replace(/\/g, '/')
: undefined;
await category.save();
return res.json({
message: 'Category has been updated',
data: category,
});
} catch (error) {
next(createError(error));
}
};