我正在对数据库进行简单的加减。我有两个值CurrentAccount
和RecentlyWithdrawn
。现在,有兴趣进行更新的值是AfterCurrentAccount
-坏名,但与我同在。 AfterCurrentAccount
公式如下。
减去AfterCurrentAccount = CurrentAccount - RecentlyWithdrawn if Credit = 0 and RecentlyWithdrawn => 1
添加AfterCurrentAccount = CurrentAccount + Credit RecentlyWithdrawn if RecentlyWithdrawn = 0 and Credit => 1
但注释:
AfterCurrentAccount
总是以等于开头 到CurrentAccount
技术堆栈:Node.js,Mongoose,Express,Body-Parser
模式:
const PostSchema = Schema({
CurrentAccount: {
type: Number,
default: 0,
required: true
},
name: {
type: String,
required: true
},
RecentlyWithdrawn:{
type: Number,
required: true
},
Credit:{
type: Number,
required: true
},
date: {
type: Date,
default: Date.now
},
AfterCurrentAccount: {
type: Number,
default: CurrentAccount
}
});
//减?
如果CurrentAccount
> 0并且'Credit'= 0
RecentlyWithdrawn
减去一个金额
router.post('/:postId', async (req, res) =>{
const post = new Post({
name: req.body.name,
CurrentAccount: req.body.CurrentAccount,
RecentlyWithdrawn: req.body. RecentlyWithdrawn,
AfterCurrentAccount: req.body.CurrentAccount,
});
try{
const updatePost = await Post.updateOne({_id: req.params.postId}, {$subtract: {CurrentAccount: req.body.CurrentAccount},{RecentlyWithdrawn: req.body.CurrentAccount});
res.json(updatePost);
}catch(err){
res.json({message: err});
}
});
//添加?
如果CurrentAccount
= 0且'Credit'> 0
RecentlyWithdrawn
减去一个金额
router.post('/:postId', async (req, res) =>{
const post = new Post({
name: req.body.name,
CurrentAccount: req.body.CurrentAccount,
RecentlyWithdrawn: req.body. RecentlyWithdrawn,
AfterCurrentAccount: req.body.CurrentAccount,
Credit: req.body.Credit,
});
try{
const updatePost = await Post.updateOne({_id: req.params.postId}, {$add: {CurrentAccount: req.body.CurrentAccount},{RecentlyWithdrawn: req.body.CurrentAccount});
res.json(updatePost);
}catch(err){
res.json({message: err});
}
});