[SERVER-35202] Parsing of count() commands with limit modifiers is counter-intuitive Created: 24/May/18  Updated: 08/Jun/18  Resolved: 08/Jun/18

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 3.6.5
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: William Byrne III Assignee: Asya Kamsky
Resolution: Won't Fix Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Case:

 Description   

MyColl has 20 documents, as count() and find().count() confirm, but adding limit(10) still returns 20:

  db.MyColl.count()
    20
 
  db.MyColl.find().count()
    20
 
  db.MyColl.find().limit(10).count()
    20

This is the correct syntax as per our documentation for cursor.count() - the default behaviour when applySkipLimit is not set to true is to ignore any skip(n) and limit(m) methods.

However, it is counter-intuitive for a user to have included the limit(10) but to also want it to be ignored. Note further that itcount() behaves differently:

  db.MyColl.find().limit(10).itcount()
    10

Conclusion: the default value for applySkipLimit should be true.



 Comments   
Comment by Asya Kamsky [ 08/Jun/18 ]

Since we will be removing shell count helper in the future, we won't be working on changing its option semantics (which are confusing, and that's one of the reasons we want to replace it with something better).

Comment by Asya Kamsky [ 01/Jun/18 ]

This is basically the same as SERVER-675 right?

 

Comment by William Byrne III [ 24/May/18 ]

It gets worse - having ANYTHING between the ( and ) for cursor.count() is interpreted as setting applySkipLimit to true, even when user intentions are clearly otherwise:

  db.MyColl.find().limit(10).count({}, {limit:5})
    10
 
  db.MyColl.find().limit(10).count({applySkipLimit:true})
    10
          
  db.MyColl.find().limit(10).count({applySkipLimit:false})
    10      

The correct way to specify applySkipLimit (see below) is not clear from our documentation:

  db.MyColl.find().limit(10).count(true)
    10      
  db.MyColl.find().limit(10).count(false)
    20      

It is also not clear to users which of the cursor.count() and db.collection.count() syntaxes will be applied to the arguments passed with count(). For example, only the last of these four commands is doing what the user expected, but the are no warning or error messages from the other three usages:

  db.MyColl.find().count({limit:5})
   20
  db.MyColl.find().count({}, {limit:5})
   20
  db.MyColl.count({limit:5})
   0
  db.filemeta.count({}, {limit:5})
   5

If (for backwards compatibility reasons) we cannot change the behaviour of the code, the least we could do is update the documentation to:

  • include examples of setting applySkipLimit to both true and false
  • make clearer to users the difference between db.collection.find().count() and db.collection.count()
Generated at Thu Feb 08 04:39:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.