Javascript node.js mongodb .. 发现(不可变)字段“_id”已被更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31775150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:10:53  来源:igfitidea点击:

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

javascriptnode.jsmongodb

提问by MeetJoeBlack

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

当我尝试使用新对象(从 xml 文件解析)更新对象时遇到一些问题,但出现以下错误:

 MongoError: exception: After applying the update to the document {_id: ObjectId('55be3c8f79bae4f80c6b17f8') , ...}, the (immutable) field '_id' was found to have been altered to _id: ObjectId('55be5f15ae
5597401724aab3')

Here is my code:

这是我的代码:

xmlFile.product_list.product.forEach(function (product) {

                    var productEntity = new Product({
                        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
                    });
                    items.push(productEntity);

                });


items.forEach(function (product) { //update or create new products

                    Product.findOneAndUpdate({product_id: product.product_id}, product, {upsert: true}, function (err) {
                        if (err)  return updateDBCallback(err);
                        updateDBCallback(null, 'saved');

                    });

                });

I tried to use hints like:

我尝试使用以下提示:

                //product._id = mongoose.Types.ObjectId(); //doesn't work
                //delete product._id // doesn't delete _id from product

but they didn't help. So I don't want to update my product._id , I just want to update the other fields.

但他们没有帮助。所以我不想更新我的 product._id ,我只想更新其他字段。

回答by Alfonso Pérez

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

当你这样做时new Product()_id会生成一个新的,以及其他东西。尝试这个:

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');

  });

});

回答by Bharath T

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});

回答by Fiehra

for anyone coming here from the question:

对于从问题来到这里的任何人:

Performing an update on the path '_id' would modify the immutable field '_id'

对路径“_id”执行更新将修改不可变字段“_id”

my problem when getting the error message was that i was not sending the userdatain my emit(user) method so my backend received an undefined object. because of this the backend tried to replace the already existing user with a user with a different or undefined id

收到错误消息时我的问题是我没有在我的发射(用户)方法中发送用户数据,所以我的后端收到了一个未定义的对象。因此,后端试图用具有不同或未定义 id 的用户替换已经存在的用户

my fix was to emit my user data in my event emitter function in order to use the userData in my updatefunction

我的解决方法是在我的事件发射器函数中发出我的用户数据,以便在我的更新函数中使用 userData

// emit function this.emit(user);

// 发射函数 this.emit(user);

// updateFunction this.profileService.update(user.id);

// updateFunction this.profileService.update(user.id);

hope it helps anyone ending up here when facing the same problem i had at a later moment in time just like myself

希望它可以帮助任何人在遇到我后来遇到的同样问题时,就像我自己一样