在 MongoDB 上,使用 "username" 上的 uniq 索引创建 "security" 集合,我正在使用 motor.motor_asynci 与 Mongo 一起工作。尝试获取与“用户名”相关的文档,如下所示:
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)
得到“TypeError:'AsyncIOMotorCursor'对象不可迭代”
由于这个集合在搜索键上有 uniq 索引,我也尝试了 find_one 但也没有工作:
user = DB.security.find_one({'username': "someuser"})
print(user)
得到:
<Future pending cb=[run_on_executor.<locals>._call_check_cancel() at /usr/local/lib/python3.8/site-packages/motor/frameworks/asyncio/__init__.py:80]>