Permalink
Browse files

SERVER-6254 - fix findAndModify to use _id on subsequent queries

  • Loading branch information...
1 parent e1f1c95 commit 4e368bff9af9effb1659a716be480bb1f82e3158 @erh erh committed Jul 3, 2012
Showing with 20 additions and 5 deletions.
  1. +10 −0 jstests/find_and_modify_server6254.js
  2. +10 −5 src/mongo/db/commands/find_and_modify.cpp
@@ -0,0 +1,10 @@
+
+t = db.find_and_modify_server6254;
+t.drop();
+
+t.insert( { x : 1 } )
+ret = t.findAndModify( { query : { x : 1 } , update : { $set : { x : 2 } } , new : true } )
+assert.eq( 2 , ret.x , tojson( ret ) )
+
+assert.eq( 1 , t.count() )
+
@@ -105,7 +105,7 @@ namespace mongo {
}
bool runNoDirectClient( const string& ns ,
- const BSONObj& query , const BSONObj& fields , const BSONObj& update ,
+ const BSONObj& queryOriginal , const BSONObj& fields , const BSONObj& update ,
bool upsert , bool returnNew , bool remove ,
BSONObjBuilder& result ) {
@@ -115,11 +115,16 @@ namespace mongo {
BSONObj doc;
- bool found = Helpers::findOne( ns.c_str() , query , doc );
+ bool found = Helpers::findOne( ns.c_str() , queryOriginal , doc );
+
+ BSONObj queryModified = queryOriginal;
+ if ( found && doc["_id"].type() )
+ queryModified = doc["_id"].wrap();
+
if ( remove ) {
_appendHelper( result , doc , found , fields );
if ( found ) {
- deleteObjects( ns.c_str() , query , true , true );
+ deleteObjects( ns.c_str() , queryModified , true , true );
}
}
else {
@@ -135,10 +140,10 @@ namespace mongo {
_appendHelper( result , doc , found , fields );
}
- updateObjects( ns.c_str() , update , query , upsert , false , true , cc().curop()->debug() );
+ updateObjects( ns.c_str() , update , queryModified , upsert , false , true , cc().curop()->debug() );
if ( returnNew ) {
- verify( Helpers::findOne( ns.c_str() , query , doc ) );
+ verify( Helpers::findOne( ns.c_str() , queryModified , doc ) );
_appendHelper( result , doc , true , fields );
}

0 comments on commit 4e368bf

Please sign in to comment.