當我在郵遞員中點擊洗掉方法時,我需要禁用 MongoDB 中的資料而不是完全洗掉。怎么做?
router.delete("/admin/delete_profile/:id", async (req, res) => {
try {
await SomeModel.findByIdAndDelete(req.params.id.trim());
return send(res, RESPONSE.SUCCESS);
} catch (err) {
// res.status(404).send(err.message);
return send(res, RESPONSE.UNKNOWN_ERROR);
}
});
schema.js
const { json } = require("body-parser");
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const SomeModelSchema = new Schema({
first_name: {
type: String,
required: true,
},
last_name: {
type: String,
required: true,
},
image: {
data: Buffer,
type: String,
required: true,
},
});
module.exports = mongoose.model("SomeModel", SomeModelSchema);
uj5u.com熱心網友回復:
實際上,@PawanYadav 建議的方法在我看來是一個很好的方法。在 Schema 中
宣告一個Boolean標志(默認為):isActivetrue
const SomeModelSchema = new Schema({
first_name: {
type: String,
required: true,
},
last_name: {
type: String,
required: true,
},
image: {
data: Buffer,
type: String,
required: true,
},
isActive: {
type: Boolean,
default: true,
}
});
并用于findByIdAndUpdate設定標志false以禁用資料:
try {
await SomeModel.findByIdAndUpdate(req.params.id.trim(), {
isActive: false,
});
return send(res, RESPONSE.SUCCESS);
} catch (err) {
// res.status(404).send(err.message);
return send(res, RESPONSE.UNKNOWN_ERROR);
}
uj5u.com熱心網友回復:
您可以isActive:true在資料庫中保留一個鍵,用于軟洗掉目的。當您點擊洗掉 api 時,您可以簡單地將這個鍵更改為 false。這樣您可以將此檔案與其他檔案區分開來,當您想要串列時,您可以使用isActive:truein檢查檔案您的查詢。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/526290.html
