[SERVER-24727] Find command returns incorrect number of documents when singleBatch is true and limit is greater than 101 Created: 22/Jun/16 Updated: 07/Nov/18 Resolved: 27/Jun/16 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Querying |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | J Rassi | Assignee: | David Storch |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Operating System: | ALL |
| Sprint: | Query 17 (07/15/16) |
| Participants: |
| Description |
|
The find command returns an incorrect number of documents when the "singleBatch" option is set to true and the "limit" option is set to a value greater than 101. In this case, the find command returns 101 documents, whereas it should return the number of documents equal to the specified "limit" value. This affects all versions of the server since the find command was first introduced (3.2.0). It also affects the use of legacy OP_QUERY reads against mongos (which use the find command internally). Reproduce as follows:
Credit goes to jeff.yemin for discovering this issue. |
| Comments |
| Comment by Rubal Jabbal [ 07/Nov/18 ] | ||||||||||||||||||||
|
Looks like this was a bug in 3.0 and was fixed in 3.2: Ref: https://jira.mongodb.org/browse/SERVER-24547 | ||||||||||||||||||||
| Comment by Rubal Jabbal [ 06/Nov/18 ] | ||||||||||||||||||||
|
@david.storch Hi! I have been recently doing a deep dive into understanding mongo internals better.
shows nreturned and nexamined 2568 docs (which is correct) while.
shows nreturned and nexamined 101 docs
shows nreturned and nexamined 1000 docs
shows nreturned and nexamined 900 docs | ||||||||||||||||||||
| Comment by David Storch [ 27/Jun/16 ] | ||||||||||||||||||||
|
I believe this behavior is intentional and will be closing this ticket as Works as Designed. When the server receives a find command with a limit of n and the singleBatch flag set to true, this means "Return at most n documents, but only return a single batch, and use the default batchSize." Since the default batchSize for the initial find command response is 101, the number of documents returned should be 101 in the case that n >= 101. This is quite different from a find command where batchSize is n and the singleBatch flag is set to true. This instead means "Return a single batch of up to n documents." In versions of the server before 3.2.0, there was no differentiation between batchSize and limit; the two were passed to the server using a single field called "ntoreturn". In these versions, cursor.batchSize(-n) and cursor.limit(-n) will both return n documents when n > 101. In contrast, versions 3.2.0 and later will never return more than 101 documents in the cursor.limit(-n) case, which I view as a bug fix. | ||||||||||||||||||||
| Comment by J Rassi [ 22/Jun/16 ] | ||||||||||||||||||||
|
I've set "Backport Requested" to v3.2, as I believe this to be a backport candidate. However, I suspect that the "singleBatch" option is not widely used. The following patch appears to fix the issue:
|