首页 > 解决方案 > 更新 mongodb 文档会插入另一个新文档

问题描述

执行后不会更新现有文档,而是插入更新内容的新文档。尝试了很多次通过另一个属性 upsert:false 来 updateOne menthod 但没有工作..

       class Product {
         constructor(title,img,offerprice,actualprice,description,id){
           this.title = title
           this.img = img
           this.offerprice = offerprice
           this.actualprice = actualprice
           this.description = description
           this._id = id ? new mongodb.ObjectId(id) : null
       }
        save(){
            const db = getDb();
         let Opr;
           if(this._id){
          Opr = db.collection('products').updateOne({ _id:this._id},{
          $set:this
            })
         }
        else{

      Opr = db.collection('products').insertOne(this)

        }
       return Opr.then(result => {
       console.log(result)
      })
       .catch(err => console.log(err));

        }

标签: node.jsmongodbexpress

解决方案


感谢您的帮助。:) 我在传递错误的 id 作为参数时犯了一个错误.. 现在它工作正常.. https://avrbrands.herokuapp.com


推荐阅读