[SERVER-78342] nReturned in slow query is incorrect when no docs are matched Created: 22/Jun/23  Updated: 29/Oct/23  Resolved: 16/Aug/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 7.1.0-rc0, 5.0.22, 7.0.3, 6.0.12

Type: Bug Priority: Major - P3
Reporter: Adi Agrawal Assignee: Adi Agrawal
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Related
Assigned Teams:
Query Execution
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v7.0, v6.0, v5.0, v4.4
Sprint: QE 2023-07-10, QE 2023-07-24, QE 2023-08-07, QE 2023-08-21
Participants:

 Description   

To reproduce this behavior:

db.c.insertMany([\{a: 1}, \{a: 2}, \{a: 3}])

Now run:

db.c.aggregate([{$addFields:{"diff": {$subtract: [100, "$a"]}}},{$match: {"diff": {$lte: 12}}}]);

The slow query log for this is:

"Slow query","attr":{"type":"command","ns":"test.m","appName":"MongoDB Shell","command":{"aggregate":"m","pipeline":[{"$addFields":{"hourDiff":{"$subtract":[100,"$a"]}}},{"$match":{"hourDiff":{"$lte":12}}}],"cursor":{},"lsid":{"id":{"$uuid":"dd4183fa-b0c3-420e-b0c7-54508c7efa96"}},"$db":"test"},"planSummary":"COLLSCAN","planningTimeMicros":798,"keysExamined":0,"docsExamined":3,"fromPlanCache":true,"cursorExhausted":true,"numYields":0,"nreturned":3,"queryHash":"E475932B","planCacheKey":"14AB0B81","queryFramework":"sbe","reslen":95,"locks":{"FeatureCompatibilityVersion":{"acquireCount":{"r":2}},"Global":{"acquireCount":{"r":2}}},"storage":{},"cpuNanos":3908578,"remote":"127.0.0.1:56992","protocol":"op_msg","durationMillis":3}}

This will give nReturned = 3 which is the total number of docs and not the number of docs matched. Note that this does not happen when there is only a match stage. It happens if addFields comes before match (I haven't checked other stages that could potentially cause the same issue)



 Comments   
Comment by Githook User [ 12/Oct/23 ]

Author:

{'name': 'Adityavardhan Agrawal', 'email': 'adi.agrawal@mongodb.com', 'username': 'Adityav369'}

Message: SERVER-78342 Fix nReturned in slow query logs

(cherry picked from commit 6fe23ef017834f0c962f6e8672fd75f2f2e652b1)
Branch: v5.0
https://github.com/mongodb/mongo/commit/88537a7444ce87423213da6b04cb5f8ca4b305b5

Comment by Githook User [ 11/Oct/23 ]

Author:

{'name': 'Adityavardhan Agrawal', 'email': 'adi.agrawal@mongodb.com', 'username': 'Adityav369'}

Message: SERVER-78342 Fix nReturned in slow query logs

(cherry picked from commit 6fe23ef017834f0c962f6e8672fd75f2f2e652b1)
Branch: v6.0
https://github.com/mongodb/mongo/commit/c1dc02e3790457f6b6704f9f06a3e645db66a6af

Comment by Githook User [ 11/Oct/23 ]

Author:

{'name': 'Adityavardhan Agrawal', 'email': 'adi.agrawal@mongodb.com', 'username': 'Adityav369'}

Message: SERVER-78342 Fix nReturned in slow query logs

(cherry picked from commit 6fe23ef017834f0c962f6e8672fd75f2f2e652b1)
Branch: v7.0
https://github.com/mongodb/mongo/commit/a4ffe93130523f697234bf44b1976b40695e319a

Comment by Githook User [ 16/Aug/23 ]

Author:

{'name': 'Adityavardhan Agrawal', 'email': 'adi.agrawal@mongodb.com', 'username': 'Adityav369'}

Message: SERVER-78342 Fix nReturned in slow query logs
Branch: master
https://github.com/mongodb/mongo/commit/6fe23ef017834f0c962f6e8672fd75f2f2e652b1

Generated at Thu Feb 08 06:38:05 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.