[SERVER-6254] FindAndModify throw exception if new:true is set Created: 29/Jun/12  Updated: 11/Jul/16  Resolved: 03/Jul/12

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 2.1.2
Fix Version/s: 2.2.0-rc0

Type: Bug Priority: Major - P3
Reporter: David O'Hara Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: findAndModify
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File NOTCRAIG.log     PNG File Screen Shot 2012-06-29 at 3.08.54 PM.png    
Operating System: ALL
Participants:

 Description   

Executing a findandmodify with the new:true option set causes exception to be thrown.

Steps to repro:

1. Add document to collection "TestPeople"

{"FirstName":"John"}

2. Call FindAndModify with $set operator & true to return new document {query:

{"FirstName":"John"}

,update:{$set:{"FirstName":"Tim"}},new:true}
3. Verify update has been done

I've also included a screenshot of my console repro of the issue.



 Comments   
Comment by auto [ 03/Jul/12 ]

Author:

{u'date': u'2012-07-03T10:10:09-07:00', u'email': u'eliot@10gen.com', u'name': u'Eliot Horowitz'}

Message: SERVER-6254 - fix findAndModify to use _id on subsequent queries
Branch: master
https://github.com/mongodb/mongo/commit/4e368bff9af9effb1659a716be480bb1f82e3158

Comment by Tad Marshall [ 29/Jun/12 ]

This reproduces for me in Windows ... including stack trace since I have .PDB files ...

Fri Jun 29 17:32:55 [FileAllocator] allocating new datafile /data/db/test.ns, filling with zeroes...
Fri Jun 29 17:32:55 [FileAllocator] creating directory /data/db/_tmp
Fri Jun 29 17:32:55 [FileAllocator] done allocating datafile /data/db/test.ns, size: 16MB,  took 0.044 secs
Fri Jun 29 17:32:55 [FileAllocator] allocating new datafile /data/db/test.0, filling with zeroes...
Fri Jun 29 17:32:55 [FileAllocator] done allocating datafile /data/db/test.0, size: 64MB,  took 0.153 secs
Fri Jun 29 17:32:55 [FileAllocator] allocating new datafile /data/db/test.1, filling with zeroes...
Fri Jun 29 17:32:55 [conn1] build index test.TestPeople { _id: 1 }
Fri Jun 29 17:32:55 [conn1] build index done.  scanned 0 total records. 0.001 secs
Fri Jun 29 17:32:55 [conn1] insert test.TestPeople keyUpdates:0 locks(micros) w:213515 218ms
Fri Jun 29 17:32:56 [FileAllocator] done allocating datafile /data/db/test.1, size: 128MB,  took 0.285 secs
Fri Jun 29 17:33:18 [conn1]  test Assertion failure Helpers::findOne( ns.c_str() , query , doc ) src\mongo\db\commands\find_and_modify.cpp 140
mongod.exe    ...\src\mongo\util\stacktrace.cpp(172)                           mongo::printStackTrace+0x101
mongod.exe    ...\src\mongo\util\assert_util.cpp(109)                          mongo::verifyFailed+0xdc
mongod.exe    ...\src\mongo\db\commands\find_and_modify.cpp(140)               mongo::CmdFindAndModify::runNoDirectClient+0x3cd
mongod.exe    ...\src\mongo\db\commands\find_and_modify.cpp(80)                mongo::CmdFindAndModify::runNoDirectClient+0x4d0
mongod.exe    ...\src\mongo\db\commands\find_and_modify.cpp(154)               mongo::CmdFindAndModify::run+0x104
mongod.exe    ...\src\mongo\db\dbcommands.cpp(1805)                            mongo::_execCommand+0x69
mongod.exe    ...\src\mongo\db\dbcommands.cpp(1963)                            mongo::execCommand+0xc4d
mongod.exe    ...\src\mongo\db\dbcommands.cpp(2025)                            mongo::_runCommands+0x3d7
mongod.exe    ...\src\mongo\db\ops\query.cpp(43)                               mongo::runCommands+0x3e
mongod.exe    ...\src\mongo\db\ops\query.cpp(919)                              mongo::runQuery+0x421
mongod.exe    ...\src\mongo\db\instance.cpp(244)                               mongo::receivedQuery+0x16d
mongod.exe    ...\src\mongo\db\instance.cpp(383)                               mongo::assembleResponse+0x2ed
mongod.exe    ...\src\mongo\db\db.cpp(193)                                     mongo::MyMessageHandler::process+0xf7
mongod.exe    ...\src\mongo\util\net\message_server_port.cpp(87)               mongo::pms::threadRun+0x56a
mongod.exe    ...\src\third_party\boost\libs\thread\src\win32\thread.cpp(180)  boost::`anonymous namespace'::thread_start_function+0x21
mongod.exe    f:\dd\vctools\crt_bld\self_64_amd64\crt\src\threadex.c(314)      _callthreadstartex+0x17
mongod.exe    f:\dd\vctools\crt_bld\self_64_amd64\crt\src\threadex.c(292)      _threadstartex+0x7f
kernel32.dll                                                                   BaseThreadInitThunk+0xd
Fri Jun 29 17:33:21 [conn1] command test.$cmd command: { findandmodify: "TestPeople", query: { FirstName: "John" }, update: { $set: { FirstName: "Tim" } }, new: true } update: { $set: { FirstName: "Tim" } } ntoreturn:1 nscanned:1 nupdated:1 keyUpdates:0 locks(micros) w:5342714 reslen:126 2578ms

The code's logic seems flawed. We are using findAndModify to modify one of the fields that we used to locate the document, so it is natural that we can't find it again using the same keys ... we just changed the keys.

It also repros the same way for me in Linux:

Fri Jun 29 17:41:33 [conn1] build index test.TestPeople { _id: 1 }
Fri Jun 29 17:41:33 [conn1] build index done.  scanned 0 total records. 0 secs
Fri Jun 29 17:42:01 [conn1]  test Assertion failure Helpers::findOne( ns.c_str() , query , doc ) src/mongo/db/commands/find_and_modify.cpp 140
0x750d73 0x59f258 0x7219ea 0x721e10 0x722ea9 0x5c910f 0x5c9f89 0x5caa3d 0x5f73ba 0x5fae3b 0x6f292d 0x590de0 0x8d573b 0x7f4d87462e9a 0x7f4d869804bd 
 ./mongod(_ZN5mongo15printStackTraceERSo+0x23) [0x750d73]
 ./mongod(_ZN5mongo12verifyFailedEPKcS1_j+0xb8) [0x59f258]
 ./mongod(_ZN5mongo16CmdFindAndModify17runNoDirectClientERKSsRKNS_7BSONObjES5_S5_bbbRNS_14BSONObjBuilderE+0x37a) [0x7219ea]
 ./mongod(_ZN5mongo16CmdFindAndModify17runNoDirectClientERKSsRNS_7BSONObjEiRSsRNS_14BSONObjBuilderEb+0x410) [0x721e10]
 ./mongod(_ZN5mongo16CmdFindAndModify3runERKSsRNS_7BSONObjEiRSsRNS_14BSONObjBuilderEb+0xe69) [0x722ea9]
 ./mongod(_ZN5mongo12_execCommandEPNS_7CommandERKSsRNS_7BSONObjEiRNS_14BSONObjBuilderEb+0x3f) [0x5c910f]
 ./mongod(_ZN5mongo11execCommandEPNS_7CommandERNS_6ClientEiPKcRNS_7BSONObjERNS_14BSONObjBuilderEb+0xad9) [0x5c9f89]
 ./mongod(_ZN5mongo12_runCommandsEPKcRNS_7BSONObjERNS_11_BufBuilderINS_16TrivialAllocatorEEERNS_14BSONObjBuilderEbi+0x26d) [0x5caa3d]
 ./mongod(_ZN5mongo11runCommandsEPKcRNS_7BSONObjERNS_5CurOpERNS_11_BufBuilderINS_16TrivialAllocatorEEERNS_14BSONObjBuilderEbi+0x3a) [0x5f73ba]
 ./mongod(_ZN5mongo8runQueryERNS_7MessageERNS_12QueryMessageERNS_5CurOpES1_+0xb8b) [0x5fae3b]
 ./mongod(_ZN5mongo16assembleResponseERNS_7MessageERNS_10DbResponseERKNS_11HostAndPortE+0xa6d) [0x6f292d]
 ./mongod(_ZN5mongo16MyMessageHandler7processERNS_7MessageEPNS_21AbstractMessagingPortEPNS_9LastErrorE+0x90) [0x590de0]
 ./mongod(_ZN5mongo3pms9threadRunEPNS_13MessagingPortE+0x3eb) [0x8d573b]
 /lib/x86_64-linux-gnu/libpthread.so.0(+0x7e9a) [0x7f4d87462e9a]
 /lib/x86_64-linux-gnu/libc.so.6(clone+0x6d) [0x7f4d869804bd]

Comment by Scott Hernandez (Inactive) [ 29/Jun/12 ]

Can you send the server logs? I cannot reproduce this with 2.1.2 on my system.

> db.TestPeople.insert({"FirstName":"John"})
> db.TestPeople.findAndModify({query:{"FirstName":"John"},update:{$set:{"FirstName":"Tim"}},new:true})
{ "FirstName" : "Tim", "_id" : ObjectId("4fee0e4ee285a7d17b0660c9") }

Generated at Thu Feb 08 03:11:11 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.