Skip to content

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

@Winedays

Description

@Winedays

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions