MongoDB: updateOne() duplicate key exception

536 views Asked by At

I am trying to save a record in cosmos db using com.mongodb.client.MongoCollection.updateOne() with UPSERT flag "true". But I am getting duplicate key _id error and on retry the same object saves into db. I am unable to figure out the root cause of this error.

Below are the environmental details

  • Azure cosmos version 3.6
  • mongo driver version 2.1.6
  • Unique constraint on all index field is set to false

Code

mongoCollection.updateOne(filter, new Document("$set", doc), updateOptions.upsert(true));

Exception

E11000 duplicate key error collection: my-db.myCollection. Failed _id or unique index constraint.
com.mongodb.MongoWriteException:
   at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest (MongoCollectionImpl.java967)
   at com.mongodb.client.internal.MongoCollectionImpl.executeUpdate (MongoCollectionImpl.java951)
   at com.mongodb.client.internal.MongoCollectionImpl.updateOne (MongoCollectionImpl.java613)
   at com.xyz.util.myclass.myMethod (myClass.java162)
   at com.xyz.util.myclass.myMethod (myClass.java73)
   at com.xyz.process.myclass.myMethod (myClass.java135)
   at com.xyz.process.myclass.myMethod (myClass.java87)
   at com.xyz.process.myclass.myMethod (myClass.java51)
   at com.xyz.springcloudflow.myclass.myMethod (myClass.java34)
   at sun.reflect.GeneratedMethodAccessor129.invoke
   at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java43)
   at java.lang.reflect.Method.invoke (Method.java498)
   at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke (InvocableHandlerMethod.java171)
   at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke (InvocableHandlerMethod.java120)
   at org.springframework.cloud.stream.binding.StreamListenerMessageHandler.handleRequestMessage (StreamListenerMessageHandler.java55)
   at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal (AbstractReplyProducingMessageHandler.java123)
   at org.springframework.integration.handler.AbstractMessageHandler.handleMessage (AbstractMessageHandler.java169)
   at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch (AbstractDispatcher.java115)
   at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch (UnicastingDispatcher.java132)
   at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch (UnicastingDispatcher.java105)
   at org.springframework.integration.channel.AbstractSubscribableChannel.doSend (AbstractSubscribableChannel.java73)
   at org.springframework.integration.channel.AbstractMessageChannel.send (AbstractMessageChannel.java453)
   at org.springframework.integration.channel.AbstractMessageChannel.send (AbstractMessageChannel.java401)
   at org.springframework.messaging.core.GenericMessagingTemplate.doSend (GenericMessagingTemplate.java187)
   at org.springframework.messaging.core.GenericMessagingTemplate.doSend (GenericMessagingTemplate.java166)
   at org.springframework.messaging.core.GenericMessagingTemplate.doSend (GenericMessagingTemplate.java47)
   at org.springframework.messaging.core.AbstractMessageSendingTemplate.send (AbstractMessageSendingTemplate.java109)
   at org.springframework.integration.endpoint.MessageProducerSupport.sendMessage (MessageProducerSupport.java205)
   at org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter.sendMessageIfAny (KafkaMessageDrivenChannelAdapter.java369)
   at org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter.access$400 (KafkaMessageDrivenChannelAdapter.java74)
   at org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter$IntegrationRecordMessageListener.onMessage (KafkaMessageDrivenChannelAdapter.java431)
   at org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter$IntegrationRecordMessageListener.onMessage (KafkaMessageDrivenChannelAdapter.java402)
   at org.springframework.kafka.listener.adapter.RetryingMessageListenerAdapter.lambda$onMessage$0 (RetryingMessageListenerAdapter.java120)
   at org.springframework.retry.support.RetryTemplate.doExecute (RetryTemplate.java287)
   at org.springframework.retry.support.RetryTemplate.execute (RetryTemplate.java211)
   at org.springframework.kafka.listener.adapter.RetryingMessageListenerAdapter.onMessage (RetryingMessageListenerAdapter.java114)
   at org.springframework.kafka.listener.adapter.RetryingMessageListenerAdapter.onMessage (RetryingMessageListenerAdapter.java40)
   at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeOnMessage (KafkaMessageListenerContainer.java1275)
   at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeOnMessage (KafkaMessageListenerContainer.java1258)
   at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeRecordListener (KafkaMessageListenerContainer.java1219)
   at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeWithRecords (KafkaMessageListenerContainer.java1200)
   at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeRecordListener (KafkaMessageListenerContainer.java1120)
   at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeListener (KafkaMessageListenerContainer.java935)
   at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.pollAndInvoke (KafkaMessageListenerContainer.java751)
   at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.run (KafkaMessageListenerContainer.java700)
   at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java511)
   at java.util.concurrent.FutureTask.run (FutureTask.java266)
   at java.lang.Thread.run (Thread.java748)

0

There are 0 answers