[SERVER-41829] findAndModify ignores filter expressions that are not objects Created: 19/Jun/19  Updated: 29/Oct/23  Resolved: 12/Jul/19

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: 3.6.14, 4.2.0-rc3, 4.0.12, 3.4.23, 4.3.1

Type: Bug Priority: Critical - P2
Reporter: McKittrick Swindle Assignee: Arun Banala
Resolution: Fixed Votes: 0
Labels: query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux OS, inside Docker (mongo)


Issue Links:
Backports
Depends
Documented
is documented by DOCS-12884 Investigate changes in SERVER-41829: ... Closed
Backwards Compatibility: Minor Change
Backport Requested:
v4.2, v4.0, v3.6, v3.4
Steps To Reproduce:

db.test.drop();
db.test.insert({});
assert.commandFailed(db.test.findOneAndDelete('foobar'));    // FAILS
assert.eq(db.test.find().itcount(), 1);     // FAILS

Sprint: Query 2019-07-15
Participants:

 Description   

I have some basic users in a collection created by mongoose.  My program code created one incorrectly, so I went into the command line to fix it. (its not a big application). I went to use the db.colleciton.findOneAndDelete(...) first, but it deleted the wrong entry.

I was confused at first, but I didn't craft the query in the argument right to match a record, and it should have errored out. When I use the exact same query string in db.collection.findOne(...) it errors and doesn't continue, but with db.collection.findOneAndDelete(...) it does not error, and appears to delete the first record it found.

 

> db.users
autoradar.users
> db.users.find()
{ "_id" : ObjectId("5cb781472973f80012ea11e5"), "registered" : true, "username" : "hannah", "password" : "-redacted-", "__v" : 0 }
{ "_id" : ObjectId("5d0951717c815e0012af79df"), "registered" : true, "username" : "Harshil ", "password" : "-redacted-", "__v" : 0 }
{ "_id" : ObjectId("5cb4db582973f80012ea11d8"), "registered" : true, "username" : "mac", "password" : "-redacted-", "__v" : 0 }
> db.users.findOne('{ "username" : "mac" }')
2019-06-19T14:14:02.120+0000 E QUERY    [js] Error: error: {
        "ok" : 0,
        "errmsg" : "SyntaxError: missing ; before statement :\nfunctionExpressionParser@src/mongo/scripting/mozjs/mongohelpers.js:48:25\n",
        "code" : 139,
        "codeName" : "JSInterpreterFailure"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCommandCursor@src/mongo/shell/query.js:708:1
DBQuery.prototype._exec@src/mongo/shell/query.js:113:28
DBQuery.prototype.hasNext@src/mongo/shell/query.js:288:5
DBCollection.prototype.findOne@src/mongo/shell/collection.js:260:10
@(shell):1:1
> db.users.findOneAndDelete('{ "username" : "mac" }')
{
        "_id" : ObjectId("5cb781472973f80012ea11e5"),
        "registered" : true,
        "username" : "hannah",
        "password" : "-redacted-",
        "__v" : 0
}
> db.users.find()
{ "_id" : ObjectId("5d0951717c815e0012af79df"), "registered" : true, "username" : "Harshil ", "password" : "-redacted-", "__v" : 0 }
{ "_id" : ObjectId("5cb4db582973f80012ea11d8"), "registered" : true, "username" : "mac", "password" : "-redacted-", "__v" : 0 }



 Comments   
Comment by Githook User [ 31/Jul/19 ]

Author:

{'name': 'Arun Banala', 'username': 'banarun', 'email': 'arun.banala@mongodb.com'}

Message: SERVER-41829 findAndModify ignores filter expressions that are not objects

(cherry picked from commit de511c6bbf22d662912f228a3ac7a8e7a8bc3c61)
(cherry picked from commit 0f41fa2d430cca4912820ed0fe929e193b005e94)
Branch: v3.4
https://github.com/mongodb/mongo/commit/f0fd2aeb09c78386456d8da88d88cfdaad440c81

Comment by Githook User [ 31/Jul/19 ]

Author:

{'name': 'Arun Banala', 'email': 'arun.banala@mongodb.com', 'username': 'banarun'}

Message: SERVER-41829 findAndModify ignores filter expressions that are not objects

(cherry picked from commit de511c6bbf22d662912f228a3ac7a8e7a8bc3c61)
Branch: v3.6
https://github.com/mongodb/mongo/commit/0f41fa2d430cca4912820ed0fe929e193b005e94

Comment by Githook User [ 31/Jul/19 ]

Author:

{'name': 'Arun Banala', 'email': 'arun.banala@mongodb.com', 'username': 'banarun'}

Message: SERVER-41829 findAndModify ignores filter expressions that are not objects

(cherry picked from commit de511c6bbf22d662912f228a3ac7a8e7a8bc3c61)
Branch: v4.0
https://github.com/mongodb/mongo/commit/c442fc5c84421ce4ca26f0e33b829705070c94d3

Comment by Githook User [ 12/Jul/19 ]

Author:

{'name': 'Arun Banala', 'email': 'arun.banala@mongodb.com', 'username': 'banarun'}

Message: SERVER-41829 findAndModify ignores filter expressions that are not objects

(cherry picked from commit de511c6bbf22d662912f228a3ac7a8e7a8bc3c61)
Branch: v4.2
https://github.com/mongodb/mongo/commit/ee725c6f30e7a2bde6fd8b012cfbf7aba95dc9e3

Comment by Githook User [ 12/Jul/19 ]

Author:

{'name': 'Arun Banala', 'email': 'arun.banala@mongodb.com', 'username': 'banarun'}

Message: SERVER-41829 findAndModify ignores filter expressions that are not objects
Branch: master
https://github.com/mongodb/mongo/commit/de511c6bbf22d662912f228a3ac7a8e7a8bc3c61

Comment by Asya Kamsky [ 30/Jun/19 ]

I originally thought there was a driver/shell related issue but this appears to be on the server:

command test.sort appName: "MongoDB Shell" command: findAndModify { findandmodify: "sort", query: "abc", update: { $set: { x: 1.0 } }, upsert: false, new: false, lsid: { id: UUID("ec143ba0-4bf4-444d-8bd4-63c3935ec1be") }, $db: "test" } planSummary: COLLSCAN keysExamined:0 docsExamined:1 nMatched:1 nModified:0 numYields:0 reslen:161 locks:{ ParallelBatchWriter: { acquireCount: { r: 1 } }, ReplicationStateTransition: { acquireCount: { w: 1 } }, Global: { acquireCount: { w: 1 } }, Database: { acquireCount: { w: 1 } }, Collection: { acquireCount: { w: 1 } }, Mutex: { acquireCount: { r: 1 } } } flowControl:{ acquireCount: 1 } storage:{} protocol:op_msg 0ms

You can see the query filter is nonsense: query: "abc"

I think we should fix and backport if it's not too destabilizing.

Generated at Thu Feb 08 04:58:47 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.