猫鼬+ NodeJ:添加和减去两个字段并给出响应

时间:2019-07-20 22:28:43

标签: node.js express mongoose mongoose-schema body-parser

我正在对数据库进行简单的加减。我有两个值CurrentAccountRecentlyWithdrawn。现在,有兴趣进行更新的值是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});
   }
});

0 个答案:

没有答案