[SERVER-675] count() broken with $min and $max Created: 25/Feb/10  Updated: 08/Jun/18  Resolved: 08/Jun/18

Status: Closed
Project: Core Server
Component/s: Querying, Shell
Affects Version/s: 2.4.7, 2.5.4
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Dwight Merriman Assignee: Asya Kamsky
Resolution: Won't Fix Votes: 1
Labels: query_triage
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File testminmax.js    
Issue Links:
Duplicate
duplicates SERVER-9548 Support $min/max in count command Closed
Participants:

 Description   

Prepending description from SERVER 9548

> db.docs.drop()
> for (var i=0; i<10; i++) { db.docs.insert({number: i}) }
> db.docs.find()
{ "_id" : ObjectId("51834d678d284e5c5e3ea7e3"), "number" : 0 }
{ "_id" : ObjectId("51834d678d284e5c5e3ea7e4"), "number" : 1 }
{ "_id" : ObjectId("51834d678d284e5c5e3ea7e5"), "number" : 2 }
{ "_id" : ObjectId("51834d678d284e5c5e3ea7e6"), "number" : 3 }
{ "_id" : ObjectId("51834d678d284e5c5e3ea7e7"), "number" : 4 }
{ "_id" : ObjectId("51834d678d284e5c5e3ea7e8"), "number" : 5 }
{ "_id" : ObjectId("51834d678d284e5c5e3ea7e9"), "number" : 6 }
{ "_id" : ObjectId("51834d678d284e5c5e3ea7ea"), "number" : 7 }
{ "_id" : ObjectId("51834d678d284e5c5e3ea7eb"), "number" : 8 }
{ "_id" : ObjectId("51834d678d284e5c5e3ea7ec"), "number" : 9 }
> db.docs.count()
10

> db.docs.find().max({number: 4})
{ "_id" : ObjectId("51834d678d284e5c5e3ea7e3"), "number" : 0 }
{ "_id" : ObjectId("51834d678d284e5c5e3ea7e4"), "number" : 1 }
{ "_id" : ObjectId("51834d678d284e5c5e3ea7e5"), "number" : 2 }
{ "_id" : ObjectId("51834d678d284e5c5e3ea7e6"), "number" : 3 }

So far so good.

> db.docs.find().max({number: 4}).count()
10

I would like this count to return 4. Instead it returns 10.

----------
(original description)
see jstests/testminmax.js

wrong result

if this is hard to fix and mainly for mongos, we could uassert "not implemented", but we can't return wrong answer.



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

Since we are going to remove the count shell helper in the future, we won't work on this.

Comment by Dwight Merriman [ 31/Mar/17 ]

The shell converts the find() into a

{ count : ... }

command when the count() helper runs.

Given that the count command has optional limit: and skip: parameters, i think the shell should forward them through, and it is not, at least not in the v3.2.x shell i just ran. It should be easy to modify _convertToCountCmd() to do this (which is called by count()).

// limit: and skip: not passed through.
// also fields: is extraneous:
//
> db.foo.find(

{x:3}

).limit(4).skip(2)._convertToCountCmd()
{ "count" : "foo", "query" :

{ "x" : 3 }

, "fields" : { } }

Comment by Asya Kamsky [ 31/Mar/17 ]

Is it really clear that it should return 4 and not 10?

This returns 10:

db.docs.find().limit(4).count()

Comment by auto [ 25/Feb/10 ]

Author:

{'login': '', 'name': 'dwight', 'email': 'dwight@dmmb.local'}

Message: minmaxtest
http://jira.mongodb.org/browse/SERVER-675
http://github.com/mongodb/mongo/commit/fa4fd06df93c6bb115a694cbcf70d76eb6fa147a

Generated at Thu Feb 08 02:54:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.