diff --git a/client/dbclient.cpp b/client/dbclient.cpp index a2d0bd85c58..1c4f919002c 100644 --- a/client/dbclient.cpp +++ b/client/dbclient.cpp @@ -272,6 +272,23 @@ void DBClientBase::remove( const char * ns , BSONObj obj , bool justOne ){ say( toSend ); } +void DBClientBase::update( const char * ns , BSONObj query , BSONObj obj , bool upsert ){ + + BufBuilder b; + b.append( (int)0 ); // reserverd + b.append( ns ); + + b.append( (int)upsert ); + + query.appendSelfToBufBuilder( b ); + obj.appendSelfToBufBuilder( b ); + + Message toSend; + toSend.setData( dbUpdate , b.buf() , b.len() ); + + say( toSend ); +} + /* -- DBClientCursor ---------------------------------------------- */ void assembleRequest( const string &ns, BSONObj query, int nToReturn, int nToSkip, BSONObj *fieldsToReturn, int queryOptions, Message &toSend ) { diff --git a/client/dbclient.h b/client/dbclient.h index 69e9d2d29ba..49c60a03823 100644 --- a/client/dbclient.h +++ b/client/dbclient.h @@ -324,6 +324,8 @@ public: virtual void insert( const char * ns , BSONObj obj ); virtual void remove( const char * ns , BSONObj obj , bool justOne = 0 ); + + virtual void update( const char * ns , BSONObj query , BSONObj obj , bool upsert = 0 ); }; class DBClientPaired; diff --git a/client/examples/clientTest.cpp b/client/examples/clientTest.cpp index 322b62c3812..9cda1c9640f 100644 --- a/client/examples/clientTest.cpp +++ b/client/examples/clientTest.cpp @@ -35,14 +35,14 @@ int main(){ // insert, findOne testing - conn.insert( ns ,BSONObjBuilder().append( "name" , "eliot" ).append( "num" , 1 ).doneAndDecouple() ); + conn.insert( ns , BSONObjBuilder().append( "name" , "eliot" ).append( "num" , 1 ).doneAndDecouple() ); { BSONObj res = conn.findOne( ns , BSONObjBuilder().doneAndDecouple() ); assert( strstr( res.getStringField( "name" ) , "eliot" ) ); assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) ); assert( 1 == res.getIntField( "num" ) ); } - + // cursor conn.insert( ns ,BSONObjBuilder().append( "name" , "sara" ).append( "num" , 2 ).doneAndDecouple() ); @@ -76,5 +76,29 @@ int main(){ assert( count == 0 ); } + // update + { + BSONObj res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).doneAndDecouple() ); + assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) ); + + BSONObj after = BSONObjBuilder().appendElements( res ).append( "name2" , "h" ).doneAndDecouple(); + + conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).doneAndDecouple() , after ); + res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).doneAndDecouple() ); + assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) ); + assert( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).doneAndDecouple() ).isEmpty() ); + + conn.update( ns , BSONObjBuilder().append( "name" , "eliot" ).doneAndDecouple() , after ); + res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).doneAndDecouple() ); + assert( strstr( res.getStringField( "name" ) , "eliot" ) ); + assert( strstr( res.getStringField( "name2" ) , "h" ) ); + assert( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).doneAndDecouple() ).isEmpty() ); + + // upsert + conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).doneAndDecouple() , after , 1 ); + assert( ! conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).doneAndDecouple() ).isEmpty() ); + + } + cout << "client test finished!" << endl; }