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

1hdlvixo  于 2022-12-26  发布在  Go
关注(0)|答案(5)|浏览(143)

我有一些问题,当我尝试upsert我的对象与新的(解析从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')

下面是我的代码:

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

                    });

                });

我试着用这样的暗示:

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

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

mhd8tkvw

mhd8tkvw1#

当你执行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');

  });

});
q0qdq0h2

q0qdq0h22#

您可以使用以下命令:

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

px9o7tmv3#

尝试使用

const product = productEntity.toObject();
...
delete product._id;
...
Product.findOneAndUpdate({product_id: product.product_id}, product)
xqnpmsa8

xqnpmsa84#

当我检查_idString而不是ObjectId时,我得到了这个错误(在Java驱动程序中)。
正确 Package 查询:

myCoolCollection.findOneAndUpdate(Filters.eq("_id", new ObjectId(e.id)), ...
rxztt3cl

rxztt3cl5#

C #/. NET语言
如果您使用_user. ReplaceOneAsync(user =〉user. Id == id,userIn)来更新集合中的现有记录,那么请确保包含正在更新的实际记录的id,将其留空意味着您希望它在创建类的新示例时为您生成newGuid。

public class User
 {
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
 }

var userCollection = await _claim.FindAsync(user => user.Id == id);
var userToUpdate = userCollection.FirstOrDefault();

//if user exists then provide the same id(primary key) to update the record
User updateUser = new User
{
     Id = userToUpdate.id,   // same id if the class creates one if you dont provide one.
     UserId = "000ef15d8c1ee9cd62699999",
     Email = "email",
}

wait _user.ReplaceOneAsync(user => user.Id == id, updateUser);

相关问题