Skip to content

Commit c4707d3

Browse files
committed
Adding findAndModify tests for upsert=true
1 parent cc86f2f commit c4707d3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public DBObject findAndModify(DBObject query, DBObject fields, DBObject sort, bo
373373
cmd.append( "remove", remove );
374374
else {
375375
if (update != null && !update.keySet().isEmpty()) {
376-
// if 1st key doesnt start with $, then object will be inserted as is, need to check it
376+
// if 1st key doesn't start with $, then object will be inserted as is, need to check it
377377
String key = update.keySet().iterator().next();
378378
if (key.charAt(0) != '$')
379379
_checkObject(update, false, false);

src/test/com/mongodb/JavaClientTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,17 @@ public void testFindAndModify(){
898898
assertEquals( 5 , dbObj.get( "x" ));
899899
assertNull( c.findOne(new BasicDBObject( "_id" , 1 ) ));
900900

901+
// create new one with upsert and return it
902+
dbObj = c.findAndModify( new BasicDBObject( "_id" , 2 ) , null, null, false, new BasicDBObject("$set", new BasicDBObject("a", 6)), true, true);
903+
assertEquals( 2 , dbObj.keySet().size());
904+
assertEquals( 6 , dbObj.get( "a" ));
905+
assertEquals( 6 , c.findOne(new BasicDBObject( "_id" , 2 ) ).get( "a" ));
906+
907+
// create new one with upsert and don't return it
908+
dbObj = c.findAndModify( new BasicDBObject( "_id" , 3 ) , null, null, false, new BasicDBObject("$set", new BasicDBObject("b", 7)), false, true);
909+
assertNull(dbObj);
910+
assertEquals( 7 , c.findOne(new BasicDBObject( "_id" , 3 ) ).get( "b" ));
911+
901912
// test exception throwing
902913
c.insert( new BasicDBObject( "a" , 1 ) );
903914
try {

0 commit comments

Comments
 (0)