diff --git a/scripting/v8_wrapper.cpp b/scripting/v8_wrapper.cpp index b9a4530ec22..c4e6b7de9a2 100644 --- a/scripting/v8_wrapper.cpp +++ b/scripting/v8_wrapper.cpp @@ -392,7 +392,7 @@ namespace mongo { return v8::Undefined(); } - void v8ToMongoElement( BSONObjBuilder & b , v8::Handle name , const string sname , v8::Handle value ){ + void v8ToMongoElement( BSONObjBuilder & b , v8::Handle name , const string sname , v8::Handle value , int depth ){ if ( value->IsString() ){ b.append( sname.c_str() , toSTLString( value ).c_str() ); @@ -413,7 +413,7 @@ namespace mongo { } if ( value->IsArray() ){ - BSONObj sub = v8ToMongo( value->ToObject() ); + BSONObj sub = v8ToMongo( value->ToObject() , depth ); b.appendArray( sname.c_str() , sub ); return; } @@ -493,7 +493,7 @@ namespace mongo { mongo::BinDataType( obj->Get( v8::String::New( "type" ) )->ToInt32()->Value() ), dataArray ); } else { - BSONObj sub = v8ToMongo( value->ToObject() ); + BSONObj sub = v8ToMongo( value->ToObject() , depth ); b.append( sname.c_str() , sub ); } return; @@ -517,12 +517,14 @@ namespace mongo { cout << "don't know how to convert to mongo field [" << name << "]\t" << value << endl; } - BSONObj v8ToMongo( v8::Handle o ){ + BSONObj v8ToMongo( v8::Handle o , int depth ){ BSONObjBuilder b; - - v8::Handle idName = v8::String::New( "_id" ); - if ( o->HasRealNamedProperty( idName ) ){ - v8ToMongoElement( b , idName , "_id" , o->Get( idName ) ); + + if ( depth == 0 ){ + v8::Handle idName = v8::String::New( "_id" ); + if ( o->HasRealNamedProperty( idName ) ){ + v8ToMongoElement( b , idName , "_id" , o->Get( idName ) ); + } } Local names = o->GetPropertyNames(); @@ -536,10 +538,10 @@ namespace mongo { v8::Local value = o->Get( name ); const string sname = toSTLString( name ); - if ( sname == "_id" ) + if ( depth == 0 && sname == "_id" ) continue; - v8ToMongoElement( b , name , sname , value ); + v8ToMongoElement( b , name , sname , value , depth + 1 ); } return b.obj(); } diff --git a/scripting/v8_wrapper.h b/scripting/v8_wrapper.h index 1d67cf13202..838aaf4269c 100644 --- a/scripting/v8_wrapper.h +++ b/scripting/v8_wrapper.h @@ -26,10 +26,10 @@ namespace mongo { v8::Local mongoToV8( const mongo::BSONObj & m , bool array = 0 , bool readOnly = false ); - mongo::BSONObj v8ToMongo( v8::Handle o ); + mongo::BSONObj v8ToMongo( v8::Handle o , int depth = 0 ); void v8ToMongoElement( BSONObjBuilder & b , v8::Handle name , - const string sname , v8::Handle value ); + const string sname , v8::Handle value , int depth = 0 ); v8::Handle mongoToV8Element( const BSONElement &f ); v8::Function * getNamedCons( const char * name );