What is the effect of violating a unique index constraint in MongoDB's java driver?

StackOverflow https://stackoverflow.com/questions/22644846

  •  21-06-2023
  •  | 
  •  

質問

I need to ensure an insert occurs and if it fails the system should throw an error.

Do I need to check the insert occurred myself or will the Java driver throw an error if an insert is not possible?

役に立ちましたか?

解決

You will get a MongoException.DuplicateKey exception

http://api.mongodb.org/java/current/com/mongodb/MongoException.DuplicateKey.html

However, it will depend, I believe on your write concern. Since you can:

set errors ignored write concern, specify w values of -1 to your driver.

This turns off write acknowledgments.

http://docs.mongodb.org/manual/core/write-concern/

With Errors Ignored set:

With this level of write concern, the client cannot detect failed write operations.

By default, the Acknowledged write concern is enabled in the driver as of Driver Java, version 2.10.0, this was not the case in the previous release. So, check which driver you're using as well.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top