python连接mongodb数据库
from beanie import Document, init_beanie
import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
class Person(Document):
name: str
# 数据库中集合的名称
class Collection:
name = 'Person'
async def example():
# mogodb的默认连接地址
client = AsyncIOMotorClient(
"mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false")
# test是数据库的名称,这个数据需要现在mongodb中创建好
await init_beanie(database=client.test, document_models=[Person])
# 数据库中的集合的名称
alice = Person(name="Alice")
await alice.insert()
if __name__ == "__main__":
asyncio.run(example())
标签:__,name,python,mongodb,数据库,Person,import
From: https://www.cnblogs.com/zhuoss/p/16945483.html