--- mongodb-src-r1.3.5/db/update.cpp 2010-03-22 22:12:51.000000000 -0400 +++ mongodb-src-replace/db/update.cpp 2010-03-25 13:35:58.000000000 -0400 @@ -30,7 +30,7 @@ #define DEBUGUPDATE(x) const char* Mod::modNames[] = { "$inc", "$set", "$push", "$pushAll", "$pull", "$pullAll" , "$pop", "$unset" , - "$bitand" , "$bitor" , "$bit" , "$addToSet" }; + "$bitand" , "$bitor" , "$bit" , "$addToSet", "$replace" }; unsigned Mod::modNamesNum = sizeof(Mod::modNames)/sizeof(char*); bool Mod::_pullElementMatch( BSONElement& toMatch ) const { @@ -298,6 +298,26 @@ break; } + case REPLACE: { + string str(in.valuestrsafe()); + BSONObj values = elt.embeddedObject(); + BSONElement searchElem = values[0]; + BSONElement replaceElem = values[1]; + const char *search = searchElem.valuestrsafe(); + int searchSize = searchElem.valuestrsize() - 1; + const char *replace = replaceElem.valuestrsafe(); + int replaceSize = replaceElem.valuestrsize() - 1; + string::size_type pos = 0; + + while ( (pos = str.find(search, pos)) != string::npos ) { + str.replace( pos, searchSize, replace ); + pos += replaceSize; + } + + b.append( shortFieldName, str ); + break; + } + default: stringstream ss; ss << "Mod::apply can't handle type: " << op; diff -ur mongodb-src-r1.3.5/db/update.h mongodb-src-replace/db/update.h --- mongodb-src-r1.3.5/db/update.h 2010-03-22 22:12:51.000000000 -0400 +++ mongodb-src-replace/db/update.h 2010-03-25 13:36:27.000000000 -0400 @@ -32,8 +32,8 @@ */ struct Mod { // See opFromStr below - // 0 1 2 3 4 5 6 7 8 9 10 11 - enum Op { INC, SET, PUSH, PUSH_ALL, PULL, PULL_ALL , POP, UNSET, BITAND, BITOR , BIT , ADDTOSET } op; + // 0 1 2 3 4 5 6 7 8 9 10 11 12 + enum Op { INC, SET, PUSH, PUSH_ALL, PULL, PULL_ALL , POP, UNSET, BITAND, BITOR , BIT , ADDTOSET, REPLACE } op; static const char* modNames[]; static unsigned modNamesNum; @@ -251,6 +251,11 @@ } } + case 'r': { + if ( fn[2] == 'e' && fn[3] == 'p' && fn[4] == 'l' && fn[5] == 'a' && fn[6] == 'c' && fn[7] == 'e' && fn[8] == 0 ) + return Mod::REPLACE; + break; + } default: break; } uassert( 10161 , "Invalid modifier specified " + string( fn ), false );