|
21 | 21 | import com.github.fakemongo.impl.Aggregator;
|
22 | 22 | import com.github.fakemongo.impl.ExpressionParser;
|
23 | 23 | import com.github.fakemongo.impl.geo.GeoUtil;
|
| 24 | +import com.mongodb.connection.ServerVersion; |
24 | 25 | import com.mongodb.util.JSON;
|
25 | 26 | import com.vividsolutions.jts.geom.Coordinate;
|
26 | 27 |
|
@@ -442,6 +443,28 @@ public WriteConcernException writeConcernException(int code, String err) {
|
442 | 443 | return new WriteConcernException(result, fongo.getServerAddress(), WriteConcernResult.unacknowledged());
|
443 | 444 | }
|
444 | 445 |
|
| 446 | + public WriteConcernException duplicateKeyException(int code, String err, DBObject oldObject) { |
| 447 | + if(serverIsAtLeastThreeDotThree(this.fongo)) { |
| 448 | + throw duplicateKeyException(code, err); |
| 449 | + } else { |
| 450 | + if (oldObject == null) { |
| 451 | + // insert |
| 452 | + throw duplicateKeyException(code, err); |
| 453 | + } else { |
| 454 | + // update (MongoDB throws a different exception in case of an update, see issue #200) |
| 455 | + throw mongoCommandException(code, err); |
| 456 | + } |
| 457 | + } |
| 458 | + } |
| 459 | + |
| 460 | + private boolean serverIsAtLeastThreeDotThree(Fongo fongo) { |
| 461 | + return serverIsAtLeastVersion(fongo, Fongo.V3_3_SERVER_VERSION); |
| 462 | + } |
| 463 | + |
| 464 | + static boolean serverIsAtLeastVersion(Fongo fongo, final ServerVersion serverVersion) { |
| 465 | + return fongo.getServerVersion().compareTo(serverVersion) >= 0; |
| 466 | + } |
| 467 | + |
445 | 468 | public WriteConcernException duplicateKeyException(int code, String err) {
|
446 | 469 | final BsonDocument result = bsonResultNotOk(code, err);
|
447 | 470 | return new DuplicateKeyException(result, fongo.getServerAddress(), WriteConcernResult.unacknowledged());
|
|
0 commit comments