Use cursor for counting documents in inversed collections #1086
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Revisiting this once again. This change was previously introduced in #649 and subsequently reverted in #692 because it can return incorrect results in certain use cases.
Instead of ignoring elements added to the collection like before, the code now counts the elements stored in the collection. If the collection is inversed and has not been initialized, it uses a count command on the cursor to get the number of documents in the database and adds that to the number of elements in the collection. This should return accurate results as well as improve time and memory consumption considerably. Note that all variants have edge cases where the result of count() will be incorrect - this can not be avoided as long as documents are manually added to the collection.
@spantaleev I would appreciate your feedback on this. I use inverse references a lot, but wherever I use them I never manually add documents to them. It would help a lot if you could test this against the code where you previously ran into the issue of wrong counts.