motor.motor_asyncio.AsyncIOMotorClient не подключается к mongoldb

Я пытаюсь подключить локальный mongodb, как показано ниже, но показывает подключение false, а также не могу видеть какие-либо документы коллекции пользователей.

import motor
import asyncio
import motor.motor_asyncio
import pprint


class MongoAsyncClient(object):
    def __init__(self):
        self.mongo_connection = None
        # Ensure that the event loop is passed explicitly in Motor.
        asyncio.set_event_loop(None)
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)
        self.EVENT_LOOP = asyncio.get_event_loop()

    async def get_connection(self):
        return await self._get_connection()

    async def _get_connection(self):
        """Get the connection to mongo db
        :return: the client to connect to mongodb
        """
        mongo_url = "mongodb://127.0.0.1:27017"
    
        self.mongo_connection = motor.motor_asyncio.AsyncIOMotorClient(mongo_url)
        print("mongo_connection:::", self.mongo_connection)
        return self.mongo_connection
    
    async def do_find_one(self):
        mongo_conn = await self.get_connection()
        doc = mongo_conn['testing']['users'].find()
        return doc
         

async def run():
    cl = MongoAsyncClient()
    resp = await cl.do_find_one()
    print("Im here:::", resp)



#import pdb;pdb.set_trace()
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
loop.close()

Здесь распечатанные операторы, показывающие соединение False и Cursor Obj

mongo_connection::: AsyncIOMotorClient(MongoClient(host=['127.0.0.1:27017'], document_class=dict, tz_aware=False, connect=False, driver=DriverInfo(name='Motor', version='2.3.0', platform='asyncio')))

Im here::: AsyncIOMotorCursor(<pymongo.cursor.Cursor object at 0x7fadfd9c38d0>)

0 ответов

Другие вопросы по тегам