对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

日志记录和监控

在此页面上,您可以查看可复制的代码示例,这些示例展示了可用于通过PyMongo监控和日志事件的常用方法。

提示

要了解有关此页面上显示的任何方法的更多信息,请参阅每个部分中提供的链接。

要使用此页面中的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。 请务必将代码示例中的所有占位符(例如<connection string URI> )替换为 MongoDB 部署的相关值。

您可以使用以下示例应用程序来测试本页上的代码示例。 要使用示例应用程序,请执行以下步骤:

  1. 确保已安装 PyMongo。

  2. 复制以下代码并将其粘贴到新的.py文件中。

  3. 从此页面复制代码示例,并将其粘贴到文件中的指定行。

import pymongofrom pymongo import MongoClient try: uri = "<connection string URI>" client = MongoClient(uri) database = client["<database name>"] collection = database["<collection name>"] # start example code here # end example code here client.close() except Exception as e: raise Exception( "The following error occurred: ", e)
with collection.watch() as stream: for change in stream: print(change)

要学习;了解有关 watch() 方法的更多信息,请参阅《使用变更流监控数据》指南。

给本页内容打分