【问题标题】:I m trying to change to field with $set in mongoose but only one field change?我试图在猫鼬中使用 $set 更改为字段,但只有一个字段更改?
【发布时间】:2021-12-27 04:02:42
【问题描述】:
Packages.findOneAndUpdate(
{filter},
{$set:{$inc:{reviews:1}},
       rating:4},
{new:true,
 upsert:true,
 multi:true}, 
 function(err, found){
     if(!err){
       console.log("updated")
     }else{
       console.log(err)
     }
   })

只有评分发生了变化,评论根本没有增加。 评论和评分都属于同一类型,即编号。

我在控制台中收到“更新”消息。

【问题讨论】:

    标签: javascript node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    在没有$set的情况下应用$inc:

    Packages.findOneAndUpdate({filter}, {
      $set: { rating: 4 },
      $inc: { reviews: 1 }
    }, {
      new: true,
      upsert: true,
      multi: true
    });
    

    Working example

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 1970-01-01
      • 2017-02-10
      • 2021-04-17
      • 1970-01-01
      • 2018-02-24
      • 2021-09-25
      • 2018-08-26
      • 1970-01-01
      相关资源
      最近更新 更多