BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
1.91k stars 201 forks source link

[BUG] FindQuery count() function does not use session parameter #848

Open QSHolzner opened 5 months ago

QSHolzner commented 5 months ago

Describe the bug The count() function of FindQuery does not use a session and therefore runs outside of a specified transaction context. In the implementation, the function count_documents() of AsyncIOMotorCollection is called, but the session parameter is not set there.

Current implemenation

 async def count(self) -> int:
        """
        Number of found documents
        :return: int
        """
        return (
            await self.document_model.get_motor_collection().count_documents(
                self.get_filter_query()
            )
        )

Expected behavior If find() is called with a session parameter, this session should be used in the count() call.

Changed implementation This should fix the behaviour.

async def count(self) -> int:
        """
        Number of found documents
        :return: int
        """
        return (
            await self.document_model.get_motor_collection().count_documents(
                self.get_filter_query(), session=self.session 
            )
        )
roman-right commented 5 months ago

Good catch! Thank you