Skip to content

Commit a246678

Browse files
committed
JAVA-908: Add findAndModify method overloads with WriteConcern
1 parent d0062b2 commit a246678

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,29 @@ public DBObject findOne(final Object id, final DBObject projection) {
481481
* @mongodb.driver.manual reference/command/findAndModify/ Find and Modify
482482
*/
483483
public DBObject findAndModify(DBObject query, DBObject fields, DBObject sort, boolean remove, DBObject update, boolean returnNew, boolean upsert){
484-
return findAndModify(query, fields, sort, remove, update, returnNew, upsert, 0L, MILLISECONDS);
484+
return findAndModify(query, fields, sort, remove, update, returnNew, upsert, getWriteConcern());
485+
}
486+
487+
/**
488+
* Atomically modify and return a single document. By default, the returned document does not include the modifications made on the
489+
* update.
490+
*
491+
* @param query specifies the selection criteria for the modification
492+
* @param fields a subset of fields to return
493+
* @param sort determines which document the operation will modify if the query selects multiple documents
494+
* @param remove when true, removes the selected document
495+
* @param returnNew when true, returns the modified document rather than the original
496+
* @param update the modifications to apply
497+
* @param upsert when true, operation creates a new document if the query returns no documents
498+
* @param writeConcern the write concern to apply to this operation
499+
* @return the document as it was before the modifications, unless {@code returnNew} is true, in which case it returns the document
500+
* after the changes were made
501+
* @throws MongoException
502+
* @mongodb.driver.manual reference/command/findAndModify/ Find and Modify
503+
*/
504+
public DBObject findAndModify(DBObject query, DBObject fields, DBObject sort, boolean remove, DBObject update, boolean returnNew,
505+
boolean upsert, WriteConcern writeConcern){
506+
return findAndModifyImpl(query, fields, sort, remove, update, returnNew, upsert, null, 0L, MILLISECONDS, writeConcern);
485507
}
486508

487509

@@ -508,8 +530,35 @@ public DBObject findAndModify(final DBObject query, final DBObject fields, final
508530
final boolean remove, final DBObject update,
509531
final boolean returnNew, final boolean upsert,
510532
final long maxTime, final TimeUnit maxTimeUnit) {
511-
return findAndModifyImpl(query, fields, sort, remove, update, returnNew, upsert, null, maxTime, maxTimeUnit,
512-
getWriteConcern());
533+
return findAndModify(query, fields, sort, remove, update, returnNew, upsert, maxTime, maxTimeUnit, getWriteConcern());
534+
}
535+
536+
/**
537+
* Atomically modify and return a single document. By default, the returned document does not include the modifications made on the
538+
* update.
539+
*
540+
* @param query specifies the selection criteria for the modification
541+
* @param fields a subset of fields to return
542+
* @param sort determines which document the operation will modify if the query selects multiple documents
543+
* @param remove when {@code true}, removes the selected document
544+
* @param returnNew when true, returns the modified document rather than the original
545+
* @param update performs an update of the selected document
546+
* @param upsert when true, operation creates a new document if the query returns no documents
547+
* @param maxTime the maximum time that the server will allow this operation to execute before killing it. A non-zero value requires
548+
* a server version >= 2.6
549+
* @param maxTimeUnit the unit that maxTime is specified in
550+
* @param writeConcern the write concern to apply to this operation
551+
* @return the document as it was before the modifications, unless {@code returnNew} is true, in which case it returns the document
552+
* after the changes were made
553+
* @mongodb.driver.manual reference/command/findAndModify/ Find and Modify
554+
* @since 2.12.0
555+
*/
556+
public DBObject findAndModify(final DBObject query, final DBObject fields, final DBObject sort,
557+
final boolean remove, final DBObject update,
558+
final boolean returnNew, final boolean upsert,
559+
final long maxTime, final TimeUnit maxTimeUnit,
560+
final WriteConcern writeConcern) {
561+
return findAndModifyImpl(query, fields, sort, remove, update, returnNew, upsert, null, maxTime, maxTimeUnit, writeConcern);
513562
}
514563

515564
/**
@@ -537,8 +586,39 @@ public DBObject findAndModify(final DBObject query, final DBObject fields, final
537586
final boolean returnNew, final boolean upsert,
538587
final boolean bypassDocumentValidation,
539588
final long maxTime, final TimeUnit maxTimeUnit) {
589+
return findAndModify(query, fields, sort, remove, update, returnNew, upsert, bypassDocumentValidation, maxTime, maxTimeUnit,
590+
getWriteConcern());
591+
}
592+
593+
/**
594+
* Atomically modify and return a single document. By default, the returned document does not include the modifications made on the
595+
* update.
596+
*
597+
* @param query specifies the selection criteria for the modification
598+
* @param fields a subset of fields to return
599+
* @param sort determines which document the operation will modify if the query selects multiple documents
600+
* @param remove when {@code true}, removes the selected document
601+
* @param returnNew when true, returns the modified document rather than the original
602+
* @param update performs an update of the selected document
603+
* @param upsert when true, operation creates a new document if the query returns no documents
604+
* @param bypassDocumentValidation whether to bypass document validation.
605+
* @param maxTime the maximum time that the server will allow this operation to execute before killing it. A non-zero value requires
606+
* a server version >= 2.6
607+
* @param maxTimeUnit the unit that maxTime is specified in
608+
* @param writeConcern the write concern to apply to this operation
609+
* @return the document as it was before the modifications, unless {@code returnNew} is true, in which case it returns the document
610+
* after the changes were made
611+
* @mongodb.driver.manual reference/command/findAndModify/ Find and Modify
612+
* @since 2.14.0
613+
*/
614+
public DBObject findAndModify(final DBObject query, final DBObject fields, final DBObject sort,
615+
final boolean remove, final DBObject update,
616+
final boolean returnNew, final boolean upsert,
617+
final boolean bypassDocumentValidation,
618+
final long maxTime, final TimeUnit maxTimeUnit,
619+
final WriteConcern writeConcern) {
540620
return findAndModifyImpl(query, fields, sort, remove, update, returnNew, upsert, bypassDocumentValidation, maxTime, maxTimeUnit,
541-
getWriteConcern());
621+
writeConcern);
542622
}
543623

544624
/**

src/test/com/mongodb/DBCollectionTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,18 +680,15 @@ public void testWriteConcernExceptionOnFindAndModify() throws UnknownHostExcepti
680680
checkServerVersion(3.2);
681681
assumeTrue(isReplicaSet(getMongoClient()));
682682
ObjectId id = new ObjectId();
683-
collection.setWriteConcern(new WriteConcern(5, 1));
684683
try {
685684
collection.findAndModify(new BasicDBObject("_id", id), null, null, false,
686685
new BasicDBObject("$set", new BasicDBObject("x", 1)),
687-
true, true);
686+
true, true, new WriteConcern(5, 1));
688687
fail("Expected findAndModify to error");
689688
} catch (WriteConcernException e) {
690689
assertNotNull(e.getServerAddress());
691690
assertNotNull(e.getErrorMessage());
692691
assertTrue(e.getCode() > 0);
693-
} finally {
694-
collection.setWriteConcern(WriteConcern.ACKNOWLEDGED);
695692
}
696693
}
697694

0 commit comments

Comments
 (0)