Upgrading Java MongoDriver 3.x to 4.x leads to lock with aggregate CountStrategy

Hey there,

I just upgraded our Spring Boot Application and therefore went with the new driver. After a long journey of debugging, I figured out that somehow the MongoCollections count method is (at least for me) broken. It offers two CountStrategys, whereas AGGREGATE is chosen by default but this leads to a never finishing response. The other option, COMMAND, does work. Also other queries like find works, it just this combination which does lead to a non starting application.
I’m not sure if this question is too specific here, but what do i miss? Are there some properties my running mongo 4.0 instance is not fulfilling or why does this (new?) count method is not doing what it is supposed to be.

Best, Richard

Hi Richard,

Can you post a working code example somewhere that we can take a look at? I’d like to see the path through which the MongoCollection#count is invoked. The answer to that will help us arrive at the best solution to your problem.

For background: MongoCollection#count was deprecated in 3.x and no longer included in 4.x. There are two replacements, depending on the behavior you want. If you want an accurate count of the documents in the collection (which MongoCollection#count provided if and only if you included a non-empty query filter), then you should use MongoCollection#countDocuments. If you want an estimated count of the documents in the collection (which MongoCollection#count provided if and only if you included an empty query filter), then you should use MongoCollection#estimatedDocumentCount

My hypothesis is that Spring Data MongoDB replaced use of MongoCollection#count with MongoCollection#countDocuments instead of MongoCollection#estimatedDocumentCount, and that’s why you’re seeing the behavior change.

Thanks,
Jeff

2 Likes

Hey, thanks for your fast reply. Your hypothesis seems to be right, here is a Picture of the stacktrace reaching executeCount function.

After application startup, the same call does seem to work, at least for me thats strange…

The query filter here for this count is empty in both cases, the working and non-working call…

It’s not that countDocuments won’t ever complete, it’s just that it requires a complete scan of the collection to do so, whereas estimatedDocumentCount just uses index metadata to provide an estimated count.

1 Like

Okay, that explains why it’s slow on testsystem (docs ~200k) and horrible on bigger testsystem (19kk docs). I realized I tried different different collections, so even after startup its horribly slow using count via spring. I think I’ll open a bug ticket on spring mongo…

OK, drop the link to the Spring ticket in here so I can comment on it if necessary.

1 Like

https://jira.spring.io/browse/DATAMONGO-2669