文档首页 → 开发应用程序 → Python 驱动程序 → PyMongo
连接到 MongoDB
1
创建您的 PyMongo 应用程序
将以下代码复制并粘贴到quickstart.py
文件中
from pymongo import MongoClient uri = "<connection string URI>" client = MongoClient(uri) try: database = client.get_database("sample_mflix") movies = database.get_collection("movies") # Query for a movie that has the title 'Back to the Future' query = { "title": "Back to the Future" } movie = movies.find_one(query) print(movie) client.close() except Exception as e: raise Exception("Unable to find the document due to the following error: ", e)
完成这些步骤后,您将拥有一个可工作的应用程序,该应用程序使用驱动程序连接到您的 MongoDB 部署,对示例数据运行查询,并打印出结果。
注意
如果您在此步骤中遇到问题,请在MongoDB 社区论坛 中寻求帮助或使用右侧或右下角的评价此页面 标签提交反馈。
下一步