node.js mongodb .. the (immutable) field '_id' was found to have been altered

前端 未结 3 344
时光说笑
时光说笑 2020-12-31 00:08

I have some problem when I try to upsert my object with new ones(parsed from xml file),but I got the following error:

 MongoError: exception: After applying         


        
相关标签:
3条回答
  • 2020-12-31 00:56

    You can use this:

    Product.replaceOne({ _id: 55be3c8f79bae4f80c6b17f8}, {                         
       product_id: parseInt(product.id),
       parent_id: parseInt(product.parent_id),
       sku: product.sku,
       title: product.title,
       pricenormal: parseFloat(product.price_normal),
       pricewholesale: parseFloat(product.price_wholesale),
       pricewholesalelarge: parseFloat(product.price_wholesale_large),
       pricesale: parseFloat(product.price_sale),
       weight: parseFloat(product.weight),
       volume: parseFloat(product.volume),
       unittype: product.unit_type,
       status: product.status,
       datecreating: product.datecreating,
       sp: product.sp,
       guaranteeext: product.guarantee_ext,
       used: product.used,
       statusfull: product.status_full,
       description: null,
       url: null});
    
    0 讨论(0)
  • 2020-12-31 00:57

    When you do new Product(), a new _id is generated, along with other things. Try this:

    items.forEach(function (product) { //update or create new products
    
      // Es6 assign
      var productToUpdate = {};
      productToUpdate = Object.assign(productToUpdate, product._doc);
      delete productToUpdate._id;
    
      Product.findOneAndUpdate({product_id: product.product_id}, productToUpdate, 
                             {upsert: true}, function (err) {
        if (err)  return updateDBCallback(err);
           updateDBCallback(null, 'saved');
    
      });
    
    });
    
    0 讨论(0)
  • 2020-12-31 00:58

    try to use

    const product = productEntity.toObject();
    ...
    delete product._id;
    ...
    Product.findOneAndUpdate({product_id: product.product_id}, product)
    
    0 讨论(0)
提交回复
热议问题