Skip to content

Commit af9017b

Browse files
committed
JAVA-1167: Changed save and insert such that they check for the existence of the _id key rather than comparing it to null. This lets you insert a document with _id of null (of course, you can't do that more than once per collection).
1 parent 726354c commit af9017b

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

src/main/com/mongodb/DBCollection.java

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

915912
doapply( jo );
916913

917-
return id;
914+
return jo.get("_id");
918915
}
919916

920917
/**
@@ -957,22 +954,20 @@ public WriteResult save( DBObject jo, WriteConcern concern ){
957954

958955
Object id = jo.get( "_id" );
959956

960-
if ( id == null || ( id instanceof ObjectId && ((ObjectId)id).isNew() ) ){
961-
if ( id != null && id instanceof ObjectId )
962-
((ObjectId)id).notNew();
963-
if ( concern == null )
964-
return insert( jo );
965-
else
966-
return insert( jo, concern );
957+
if ((id == null && !jo.containsField("_id")) || (id instanceof ObjectId && ((ObjectId) id).isNew())) {
958+
if (concern == null) {
959+
return insert(jo);
960+
} else {
961+
return insert(jo, concern);
962+
}
967963
}
968964

969-
DBObject q = new BasicDBObject();
970-
q.put( "_id" , id );
971-
if ( concern == null )
972-
return update( q , jo , true , false );
973-
else
974-
return update( q , jo , true , false , concern );
975-
965+
DBObject q = new BasicDBObject("_id" , id);
966+
if (concern == null) {
967+
return update(q, jo, true, false);
968+
} else {
969+
return update(q, jo, true, false, concern);
970+
}
976971
}
977972

978973
// ---- DB COMMANDS ----

src/test/com/mongodb/DBCollectionTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,39 @@
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;
3435
import static org.junit.Assert.assertTrue;
3536
import static org.junit.Assert.fail;
3637
import static org.junit.Assume.assumeFalse;
3738
import static org.junit.Assume.assumeTrue;
3839

3940
public class DBCollectionTest extends TestCase {
4041

42+
@Test
43+
public void shouldCreateIdOnInsertIfThereIsNone() {
44+
BasicDBObject document = new BasicDBObject();
45+
collection.insert(document);
46+
assertEquals(document, collection.findOne());
47+
}
48+
49+
@Test
50+
public void shouldPreserveNullIdOnInsert() {
51+
BasicDBObject document = new BasicDBObject("_id", null);
52+
collection.insert(document);
53+
assertNull(document.get("_id"));
54+
assertEquals(document, collection.findOne());
55+
}
56+
57+
@Test
58+
public void saveShouldUpdateAnExistingDocumentWithNullId() {
59+
BasicDBObject document = new BasicDBObject("_id", null);
60+
collection.insert(document);
61+
document.put("x", 1);
62+
collection.save(document);
63+
assertNull(document.get("_id"));
64+
assertEquals(document, collection.findOne());
65+
}
66+
4167
@Test
4268
public void testMultiInsert() {
4369
DBCollection c = collection;

0 commit comments

Comments
 (0)