diff --git a/src/main/com/mongodb/BasicDBList.java b/src/main/com/mongodb/BasicDBList.java index 72a0f0e2fb..bc3e8ee7ee 100644 --- a/src/main/com/mongodb/BasicDBList.java +++ b/src/main/com/mongodb/BasicDBList.java @@ -43,10 +43,13 @@ public BasicDBList(){ * @throws IndexOutOfBoundsException if key cannot be parsed into an int */ public Object put( String key , Object v ){ - int i = _getInt( key ); - while ( i >= size() ) + return put(_getInt( key ), v); + } + + public Object put( int key, Object v ) { + while ( key >= size() ) add( null ); - set( i , v ); + set( key , v ); return v; } diff --git a/src/main/com/mongodb/ByteEncoder.java b/src/main/com/mongodb/ByteEncoder.java index aa67812c7c..ed8dd87d8e 100644 --- a/src/main/com/mongodb/ByteEncoder.java +++ b/src/main/com/mongodb/ByteEncoder.java @@ -203,14 +203,17 @@ private int putObject( String name , DBObject o ){ final int sizePos = _buf.position(); _buf.putInt( 0 ); // leaving space for this. set it at the end - if ( o.containsKey( "_id" ) ) - _putObjectField( "_id" , o.get( "_id" ) ); - List transientFields = null; - { - Object temp = o.get( "_transientFields" ); - if ( temp instanceof List ) - transientFields = (List)temp; + + if ( myType == OBJECT ) { + if ( o.containsKey( "_id" ) ) + _putObjectField( "_id" , o.get( "_id" ) ); + + { + Object temp = o.get( "_transientFields" ); + if ( temp instanceof List ) + transientFields = (List)temp; + } } @@ -334,13 +337,14 @@ private boolean _handleSpecialObjects( String name , DBObject o ){ return true; } - if ( o.get( Bytes.NO_REF_HACK ) != null ){ + if ( !(o instanceof List) && o.get( Bytes.NO_REF_HACK ) != null ){ o.removeField( Bytes.NO_REF_HACK ); return false; } if ( ! _dontRefContains( o ) && name != null && + !(o instanceof List) && cameFromDB( o ) ){ putDBRef( name , o.get( "_ns" ).toString() , (ObjectId)(o.get( "_id" ) ) ); return true; diff --git a/src/test/com/mongodb/DBObjectTest.java b/src/test/com/mongodb/DBObjectTest.java index b5b5fbef05..c9f7821850 100644 --- a/src/test/com/mongodb/DBObjectTest.java +++ b/src/test/com/mongodb/DBObjectTest.java @@ -68,6 +68,29 @@ public void testToMap() { assertEquals(m.get("z"), "a"); } + @Test(groups = {"basic"}) + public void testBasicDBList() { + BasicDBList l = new BasicDBList(); + l.put(10, "x"); + assertEquals(l.get("10"), "x"); + assertEquals(l.get(3), null); + l.put("10", "y"); + assertEquals(l.get("10"), "y"); + + Mongo db; + try { + db = new Mongo( "127.0.0.1" , "test" ); + } + catch (UnknownHostException e2) { + return; + } + DBCollection c = db.getCollection("dblist"); + c.drop(); + c.insert(BasicDBObjectBuilder.start().add("array", l).get()); + DBObject obj = c.findOne(); + assertEquals(obj.get("array") instanceof List, true); + } + @Test(groups = {"basic"}) public void testPutAll() { DBObject start = BasicDBObjectBuilder.start().add( "a" , 1 ).add( "b" , 2 ).get();