on MongoDB , create "security" collection with a uniq index on "username" , I'm using motor.motor_asynci for working with Mongo. trying to fetch document related to "username" as following:
from motor.motor_asyncio import AsyncIOMotorClient
def load_config() -> dict:
with open('config/config.yml') as yaml_file:
conf = yaml.load(yaml_file.read(), Loader=yaml.SafeLoader)
return conf
CONF = load_config()
## Mongo
DB_CLIENT = AsyncIOMotorClient(
host=CONF.get("databases", dict())["mongo"]["HOST"],
port=CONF.get("databases", dict())["mongo"]["PORT"],
username=CONF.get("databases", dict())["mongo"]["USER"],
password=CONF.get("databases", dict())["mongo"]["PASSWORD"],
)
DB = DB_CLIENT[CONF.get("databases", dict())["mongo"]["NAME"]]
cursor_user = DB.security.find({'username': "someuser"})
for doc in cursor_user:
print (doc)
getting "TypeError: 'AsyncIOMotorCursor' object is not iterable"
as this collection has uniq index on the search key, i also tried find_one but also not working:
user = DB.security.find_one({'username': "someuser"})
print(user)
getting:
<Future pending cb=[run_on_executor.<locals>._call_check_cancel() at /usr/local/lib/python3.8/site-packages/motor/frameworks/asyncio/__init__.py:80]>