Skip to content

Commit 201ae41

Browse files
committed
JAVA-1167: Backing out this change after deciding that it's too risky, as users may be relying on the current behaviour.
1 parent c900d68 commit 201ae41

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -905,13 +905,15 @@ public Object apply( DBObject o ){
905905
* @return the modified object {@code o}
906906
*/
907907
public Object apply( DBObject jo , boolean ensureID ){
908-
if ( ensureID && !jo.containsField("_id") ){
909-
jo.put( "_id" , ObjectId.get() );
908+
Object id = jo.get("_id");
909+
if (ensureID && id == null) {
910+
id = ObjectId.get();
911+
jo.put("_id", id);
910912
}
911913

912-
doapply( jo );
914+
doapply(jo);
913915

914-
return jo.get("_id");
916+
return id;
915917
}
916918

917919
/**
@@ -954,20 +956,25 @@ public WriteResult save( DBObject jo, WriteConcern concern ){
954956

955957
Object id = jo.get( "_id" );
956958

957-
if ((id == null && !jo.containsField("_id")) || (id instanceof ObjectId && ((ObjectId) id).isNew())) {
959+
if (id == null || (id instanceof ObjectId && ((ObjectId) id).isNew())) {
960+
if (id != null) {
961+
((ObjectId) id).notNew();
962+
}
958963
if (concern == null) {
959964
return insert(jo);
960965
} else {
961966
return insert(jo, concern);
962967
}
963968
}
964969

965-
DBObject q = new BasicDBObject("_id" , id);
970+
DBObject q = new BasicDBObject();
971+
q.put("_id", id);
966972
if (concern == null) {
967973
return update(q, jo, true, false);
968974
} else {
969975
return update(q, jo, true, false, concern);
970976
}
977+
971978
}
972979

973980
// ---- DB COMMANDS ----

src/test/com/mongodb/DBCollectionTest.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import static org.junit.Assert.assertEquals;
3232
import static org.junit.Assert.assertFalse;
3333
import static org.junit.Assert.assertNotNull;
34-
import static org.junit.Assert.assertNull;
3534
import static org.junit.Assert.assertTrue;
3635
import static org.junit.Assert.fail;
3736
import static org.junit.Assume.assumeFalse;
@@ -43,24 +42,32 @@ public class DBCollectionTest extends TestCase {
4342
public void shouldCreateIdOnInsertIfThereIsNone() {
4443
BasicDBObject document = new BasicDBObject();
4544
collection.insert(document);
45+
assertEquals(ObjectId.class, document.get("_id").getClass());
4646
assertEquals(document, collection.findOne());
4747
}
4848

4949
@Test
50-
public void shouldPreserveNullIdOnInsert() {
50+
public void shouldCreateIdOnInsertIfTheValueIsNull() {
5151
BasicDBObject document = new BasicDBObject("_id", null);
5252
collection.insert(document);
53-
assertNull(document.get("_id"));
53+
assertEquals(ObjectId.class, document.get("_id").getClass());
5454
assertEquals(document, collection.findOne());
5555
}
5656

5757
@Test
58-
public void saveShouldUpdateAnExistingDocumentWithNullId() {
58+
public void saveShouldInsertADocumentWithNullId() {
5959
BasicDBObject document = new BasicDBObject("_id", null);
60-
collection.insert(document);
61-
document.put("x", 1);
6260
collection.save(document);
63-
assertNull(document.get("_id"));
61+
assertEquals(ObjectId.class, document.get("_id").getClass());
62+
assertEquals(document, collection.findOne());
63+
}
64+
65+
@Test
66+
public void saveShouldInsertADocumentWithNewObjectId() {
67+
ObjectId newObjectId = new ObjectId();
68+
BasicDBObject document = new BasicDBObject("_id", newObjectId);
69+
collection.save(document);
70+
assertEquals(newObjectId, document.get("_id"));
6471
assertEquals(document, collection.findOne());
6572
}
6673

0 commit comments

Comments
 (0)