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.