最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如果找到,我想更新付款文档,使用更新的字段创建一个新文档,保留原始文档

网站源码admin36浏览0评论

如果找到,我想更新付款文档,使用更新的字段创建一个新文档,保留原始文档

如果找到,我想更新付款文档,使用更新的字段创建一个新文档,保留原始文档

 const payment = await paymentsSchema.findOne({ student: s_id, guardian: g_id }, {},
          { sort: { 'createdAt': -1 } })
        console.log(payment)
        payment.balance = (payment.balance + settings.overtime_rate)
       
        // Create a new document with the updated values
        const updatedPayment =  paymentsSchema(payment.toObject());

        // Save the new document to the database
        updatedPayment.save((err) => {
          if (err) throw err;
          console.log('Payment updated successfully');
        });

我希望在发布此更新后有 2 个文件,原始文件和更新后的文件...

回答如下:
const payment = await paymentsSchema.findOne({ student: s_id, guardian: g_id }, {},
      { sort: { 'createdAt': -1 } },)
    console.log(payment)
    payment.balance = (payment.balance + settings.overtime_rate)
 
    // Create a new document with the updated values
    const updatedPayment = new paymentsSchema({
      balance: payment.balance,
     . . . //other fields you may have
    });

    // Save the new document to the database
    updatedPayment.save((err) => {
      if (err) throw err;
      console.log('Payment updated successfully');
    });
发布评论

评论列表(0)

  1. 暂无评论