创建一条增加计数的路径
make a path that increments the count
我正在尝试发出一个 post 请求,该请求将使用 express 和 mongoose 增加我的模式,
这是:
const ItemSchema = new Schema({
formName: String,
inputs: [
{
inputLabel: {
type: String,
required: true
},
inputType: {
type: String,
required: true,
enum: ['text', 'color', 'date', 'email', 'tel', 'number']
},
inputValue: {
type: String,
required: true
}
}
],
numOfSubs: { type: Number, default: 0 }
});
出于我的代码目的,我想制作一条路线,每次使用它时 numOfSubs 都会增加 1,因为有一些列表,我有 ID,所以我需要搜索它,但我没有确定如何写路径
router.post('/increase', (req, res) => {
"find and increase by 1 "
});
我将像这样使用提取:
fetch('/api/items/increase', {
method: 'POST',
body: JSON.stringify({ _id }),//the ID I of the collection I want to increment
headers: {
'content-type': 'application/json'
}
});
尝试使用 mongo $inc 运算符
router.post('/increase', (req, res, next) => {
const _id = req.body._id;
MyModel.findByIdAndUpdate(_id , { $inc: {numOfSubs: 1} }, { new: true }, (err,updateRes)=>{
if(err) return next(err);
return res.json({sucess: true});
});
});
我正在尝试发出一个 post 请求,该请求将使用 express 和 mongoose 增加我的模式, 这是:
const ItemSchema = new Schema({
formName: String,
inputs: [
{
inputLabel: {
type: String,
required: true
},
inputType: {
type: String,
required: true,
enum: ['text', 'color', 'date', 'email', 'tel', 'number']
},
inputValue: {
type: String,
required: true
}
}
],
numOfSubs: { type: Number, default: 0 }
});
出于我的代码目的,我想制作一条路线,每次使用它时 numOfSubs 都会增加 1,因为有一些列表,我有 ID,所以我需要搜索它,但我没有确定如何写路径
router.post('/increase', (req, res) => {
"find and increase by 1 "
});
我将像这样使用提取:
fetch('/api/items/increase', {
method: 'POST',
body: JSON.stringify({ _id }),//the ID I of the collection I want to increment
headers: {
'content-type': 'application/json'
}
});
尝试使用 mongo $inc 运算符
router.post('/increase', (req, res, next) => {
const _id = req.body._id;
MyModel.findByIdAndUpdate(_id , { $inc: {numOfSubs: 1} }, { new: true }, (err,updateRes)=>{
if(err) return next(err);
return res.json({sucess: true});
});
});