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

[BUG] Sort by multiple fields does not work with ExpressionField #467

Closed
nadir-albajari-hs opened this issue Jan 2, 2023 · 2 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@nadir-albajari-hs
Copy link

Describe the bug
when using multiple ExpressionField only the first sorting argument reach mongo

To Reproduce

import asyncio

import structlog
from beanie import Document, init_beanie
from beanie.odm.enums import SortDirection
from motor.motor_asyncio import AsyncIOMotorClient
from pymongo import monitoring
from pymongo.monitoring import CommandStartedEvent, CommandSucceededEvent, CommandFailedEvent

logger = structlog.get_logger("beanie test")


class CommandLogger(monitoring.CommandListener):

    def started(self, event: CommandStartedEvent):
        if event.command_name == "find":
            logger.debug(
                f"mongo {event.command_name} started",
                command=event.command,
            )

    def succeeded(self, event: CommandSucceededEvent):
        pass

    def failed(self, event: CommandFailedEvent):
        pass


class MyModel(Document):
    int_number: int
    is_boolean: bool

    class Settings:
        name = "my_model"


async def main():
    await init_beanie(AsyncIOMotorClient(event_listeners=[CommandLogger()])["test_hs_das"], document_models=[MyModel])
    sort1 = [+MyModel.int_number, +MyModel.is_boolean]
    sort2 = [("int_number", SortDirection.ASCENDING), "-is_boolean"]
    logger.info(sort1)
    logger.info(sort2)
    await MyModel.find().sort(sort1).to_list()
    await MyModel.find().sort(sort2).to_list()


if __name__ == '__main__':
    asyncio.run(main())

Expected behavior
I expect that when using

await MyModel.find().sort( [+MyModel.int_number,+MyModel.is_boolean]).to_list()

the logger output will be

2023-01-02 16:19:22 [debug ] mongo find started command=SON([('find', 'my_model'), ('filter', {}), ('sort', SON([('int_number', <SortDirection.ASCENDING: 1>), ('is_boolean', <SortDirection.DESCENDING: -1>)])), ...

but is

2023-01-02 16:19:22 [debug ] mongo find started command=SON([('find', 'my_model'), ('filter', {}), ('sort', SON([('int_number', <SortDirection.ASCENDING: 1>)]))

it does work when NOT using ExpressionField

await MyModel.find().sort([("int_number", SortDirection.ASCENDING), "-is_boolean"]).to_list()
@roman-right
Copy link
Member

Hey @nadir-albajari-hs ,
Thank you for the catch. It is solved here: #468
Will be merged tomorrow

@roman-right roman-right added the bug Something isn't working label Jan 2, 2023
@roman-right roman-right self-assigned this Jan 2, 2023
@roman-right
Copy link
Member

@nadir-albajari-hs Please try the current version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants