Skip to content
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

Collation, Hint, and Comment Ignored During QuerySet Update/Delete/Aggregate #2842

Open
Winedays opened this issue Sep 6, 2024 · 0 comments · May be fixed by #2854
Open

Collation, Hint, and Comment Ignored During QuerySet Update/Delete/Aggregate #2842

Winedays opened this issue Sep 6, 2024 · 0 comments · May be fixed by #2854

Comments

@Winedays
Copy link

Winedays commented Sep 6, 2024

Hi everyone, I encountered an issue when using collation for index creation, and then querying with that collation.

I discovered this problem when using collation for index creation and performing queries. I found that when collation is set on a QuerySet, and operations like update, delete, or aggregate are performed, MongoEngine does not propagate the collation options to the corresponding PyMongo functions. As a result, the index is not used, leading to collection scans (COLLSCAN).

Initially, I discovered this issue with collation, but further inspection of the source code revealed that both hint and comment settings also suffer from the same problem.

Steps to Reproduce
Create a Document with Index Using Collation:

class MyDoc(Document):
    name = StringField()
    
    meta = {
        'indexes': [
            {'fields': ['name'], 'collation': {'locale': 'en', 'strength': 2}}
        ]
    }

Perform a Query with Collation, Then Attempt to Update or Delete Documents:

# Query with collation specified
qs = MyDoc.objects(name='alice').collation(locale='en', strength=2)

# The following operations do not pass collation, hint, or comment to PyMongo
qs.update(set__name='Alice')  # Update
qs.delete()                   # Delete
qs.aggregate([{'$group': {'_id': '$name'}}])  # Aggregate

Observe the Result in MongoDB: MongoDB performs a collection scan (COLLSCAN) instead of utilizing the index due to collation.

I noticed that hint and comment seem to be suffering from a similar problem, they are not being delivered correctly. While I haven't specifically tested it, the source code suggest these are possible.

Environment

  • MongoEngine Version: 0.28.2
  • PyMongo Version: 4.8.0
  • Python Version: 3.12
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 a pull request may close this issue.

1 participant