是否可以在MongoDB集合中创建一个字段,该字段自动将两个数字自动汇总(我的意思是我不必每次我希望它进行更新时都调用gengregate(((
例如,我有一个书对象
{
"_id": {
"$oid": "5a8f3e4895ed5e3fdca7f2ad"
},
"category": "action",
"author": "x",
"title": "action book",
"__v": 0,
"price": 4000,
"taxes": 370
}
我想创建一个 TotalPrice ,即使更新了价格或税款,也可以汇总价格和税款。假设价格每分钟都会改变。那么,如何在价格或税收变化时建立总计=价格 税款之间的关系?
Marks.findOneAndUpdate({
username: username,
year: year,
section: section,
week: week
}, {
$set: {
quizmarks: quizmarks
}
},
(err, data) => {
if (err) {
res.json({
'marks table error': err
})
} else {
User.findOneAndUpdate({
username: username,
year: year,
section: section
}, {
$inc: {
quizmarks: quizmarks,
},
$push:
{
attemptedQuizWeeks:attemptedQuizWeek
}
}, (err, userdata) => {
if (err) {
res.json({
'usertable error': err
})
} else {
res.json({
"userdata": userdata,
"data": data
})
}
})
}
})
})
希望这会有所帮助。使用$ inc在右手方面递增您想要的特定字段