E11000 error when using upsert=true in updateOne

If _id=‘1234567890’ already exists and the value of checksum has not changed, the following query will generate an E11000 error.
I understand that this is because there is no document that matches the filter criteria, so I try to create a new document with _id=‘1234567890’.

db.User.updateOne({
  _id: '1234567890', {
  'basicInfo.checksum': {$ne: '67b95c9a412f422e309c4100ac0e8fd5'}
}, {
  $set: {
    basicInfo: {
      name: 'Yamada Taro', 
      age: 30,
      checksum: '67b95c9a412f422e309c4100ac0e8fd5'
    }
  }
}, {
  upsert: true
})

If the document with _id=‘1234567890’ does not exist, create a new one. If it exists and the checksum is different, we want to update it.
Is there any way to modify the query so that this error does not occur?
If there is no other way, is it not good manners to ignore the E11000 error that occurs?

Sorry if the text is wrong as I am using an automatic translation into English.

Sorry. There was an error in the example code. The correct code is as follows.

db.User.updateOne({
  _id: '1234567890',
  'basicInfo.checksum': {$ne: '67b95c9a412f422e309c4100ac0e8fd5'}
}, {
  $set: {
    basicInfo: {
      name: 'Yamada Taro', 
      age: 30,
      checksum: '67b95c9a412f422e309c4100ac0e8fd5'
    }
  }
}, {
  upsert: true
})

what you mean by this?

Hi @Kobe_W
Thanks for the reply.

The checksum is a hashed value of the value of the basicInfo field (excluding the checksum) using a certain algorithm. the same value of the checksum means that the value of the basicInfo field has not changed.

Sorry. I will close this post as I have to deal with this in other ways.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.