Javascript 如何使用mongoose在MongoDB中增加对象数组中的值
假设我的模型是这样的:Javascript 如何使用mongoose在MongoDB中增加对象数组中的值,javascript,mongodb,express,mongoose,Javascript,Mongodb,Express,Mongoose,假设我的模型是这样的: const PostSchema = new mongoose.Schema({ award: [ one: { type: Number, default: 0 }, two: { type: Number, default: 0 }, three: { type: Number, default: 0 }, four: {
const PostSchema = new mongoose.Schema({
award: [
one: {
type: Number,
default: 0
},
two: {
type: Number,
default: 0
},
three: {
type: Number,
default: 0
},
four: {
type: Number,
default: 0
}
]
)}
我是一个控制器,当post ID调用它时,它会增加主体中的奖励数量(例如“2”)
exports.awardPost = asyncHandler(async (req, res, next) => {
const post= await Post.findById({id: req.params.cardid},
if(!post){
res.status(400).json({
success: false,
message: 'Card not found'
})
}
post.update("Increment by one req.body.awardname"); <- What is the best way to do that here ?
res.status(200).json({
success: true,
card
})
});
exports.awardPost=asynchHandler(异步(请求、恢复、下一步)=>{
const post=wait post.findById({id:req.params.cardid},
如果(!post){
res.status(400).json({
成功:错,
消息:“找不到卡”
})
}
更新后(“增加一个请求主体名称”);CardSchema
是否在另一个模式的数组中?请使用实际的示例文档和必需的o/p编辑此问题。很遗憾,我的模式名称错误。不,这不是另一个模式的数组。你的标题是什么意思?在一个对象数组中?你是否尝试了$inc
运算符?是否CardSchema
在a中另一个模式的数组?请用实际的示例文档和所需的o/p编辑此问题。糟糕,我的模式名称错误。不,这不是另一个模式的数组。您的标题是什么意思?在对象数组中?您是否尝试了$inc
操作符?