Skip to content

DATAMONGO-2059 - Favor collection.countDocuments() over collection.count() which will be removed. #604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

christophstrobl
Copy link
Member

@christophstrobl christophstrobl commented Aug 17, 2018

This commit switches from simple collection.count(), operating on potentially false collection statistic, to countDocuments() using an aggregation for accurate results.
The transition required query modifications at some points because $match does not support $near nor $nearSphere but requires $geoWithin using $center or $centerSphere which does not support $minDistance (see SERVER-37043).
$geoWithin further more does not sort results by distance, but this fact can be ignored when just counting matches.

ℹ️ The change is a requirement for upgrading to the next driver version that no longer supports collection.count().

Examples:

// before
{ "location" : { "$near" : [-73.99171, 40.738868], "$maxDistance" : 1.1 } }

// after
{ "location" : { "$geoWithin" : { "$center" : [ [-73.99171, 40.738868], 1.1] } } }
// before
{ "location" : { "$near" : [-73.99171, 40.738868], "$minDistance" : 0.1, "$maxDistance" : 1.1 } }

// after
{ "$and" :[ 
    { "$nor" :[ { "location" :{ "$geoWithin" :{ "$center" :[ [-73.99171, 40.738868 ], 0.01] } } } ] },
    { "location" :{ "$geoWithin" :{ "$center" :[ [-73.99171, 40.738868 ], 1.1] } } } 
] }

…collection.countDocuments()

This commit switches from simple collection.count(), operating on potentially false collection statistic,  to countDocuments() using an aggregation for accurate results.
The transition required query modifications at some points because $match does not support $near and $nearSphere but require $geoWithin along with $center or $centerSphere which does not support $minDistance (see https://jira.mongodb.org/browse/SERVER-37043).
$geoWithin further more does not sort results by distance, but this fact can be ignored when just counting matches.

Examples:

{ location : { $near : [-73.99171, 40.738868], $maxDistance : 1.1 } }
{ location : { $geoWithin : { $center: [ [-73.99171, 40.738868], 1.1] } } }

{ location : { $near : [-73.99171, 40.738868], $minDistance : 0.1, $maxDistance : 1.1 } }
{$and :[ { $nor :[ { location :{ $geoWithin :{ $center :[ [-73.99171, 40.738868 ], 0.01] } } } ]}, { location :{ $geoWithin :{ $center :[ [-73.99171, 40.738868 ], 1.1] } } } ] }
@christophstrobl christophstrobl changed the title DATAMONGO-2059 - Fix document count for geo queries inside a MongoDB Session. DATAMONGO-2059 - Favor collection.countDocuments() over collection.count() which will be removed. Oct 1, 2019
Move query rewriting into CountQuery. Consider existing $and items during query rewrite.
mp911de pushed a commit that referenced this pull request Oct 25, 2019
…collection.countDocuments().

This commit switches from simple collection.count(), operating on potentially false collection statistic,  to countDocuments() using an aggregation for accurate results.
The transition required query modifications at some points because $match does not support $near and $nearSphere but require $geoWithin along with $center or $centerSphere which does not support $minDistance (see https://jira.mongodb.org/browse/SERVER-37043).
$geoWithin further more does not sort results by distance, but this fact can be ignored when just counting matches.

Examples:

{ location : { $near : [-73.99171, 40.738868], $maxDistance : 1.1 } }
{ location : { $geoWithin : { $center: [ [-73.99171, 40.738868], 1.1] } } }

{ location : { $near : [-73.99171, 40.738868], $minDistance : 0.1, $maxDistance : 1.1 } }
{$and :[ { $nor :[ { location :{ $geoWithin :{ $center :[ [-73.99171, 40.738868 ], 0.01] } } } ]}, { location :{ $geoWithin :{ $center :[ [-73.99171, 40.738868 ], 1.1] } } } ] }

Original pull request: #604.
mp911de added a commit that referenced this pull request Oct 25, 2019
Move query rewriting into CountQuery. Consider existing $and items during query rewrite.

Original pull request: #604.
@mp911de
Copy link
Member

mp911de commented Oct 25, 2019

That's merged and polished now.

@mp911de mp911de closed this Oct 25, 2019
@mp911de mp911de deleted the issue/DATAMONGO-2059 branch October 25, 2019 07:11
mp911de added a commit that referenced this pull request Oct 25, 2019
Move query rewriting into CountQuery. Consider existing $and items during query rewrite.

Original pull request: #604.
@doronlevari
Copy link

Can we get this one merged into 2.2.X? It doesn't seem THAT major and we're in a huge need for this, Mongo is totally crapping out on us when using "count"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants