Skip to content

Commit cbc8b6e

Browse files
committed
SERVER-6659 - fix find_and_modify when upserting a new doc and original query won't work anymore
1 parent ba7f067 commit cbc8b6e

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
t = db.find_and_modify_server6659;
3+
t.drop();
4+
5+
x = t.findAndModify({query:{f:1}, update:{$set:{f:2}}, upsert:true, new:true})
6+
assert.eq( 2, x.f );
7+
assert.eq( 2, t.findOne().f );

src/mongo/db/commands/find_and_modify.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,13 @@ namespace mongo {
171171
_appendHelper( result , doc , found , fields );
172172
}
173173

174-
updateObjects( ns.c_str() , update , queryModified , upsert , false , true , cc().curop()->debug() );
174+
UpdateResult res = updateObjects( ns.c_str() , update , queryModified , upsert , false , true , cc().curop()->debug() );
175175

176176
if ( returnNew ) {
177+
if ( ! res.existing && res.upserted.isSet() ) {
178+
queryModified = BSON( "_id" << res.upserted );
179+
}
180+
log() << "queryModified: " << queryModified << endl;
177181
verify( Helpers::findOne( ns.c_str() , queryModified , doc ) );
178182
_appendHelper( result , doc , true , fields );
179183
}

0 commit comments

Comments
 (0)