可以使用 mongodb 驱动程序提供的 `ChangeStream` 功能来监听 mongodb 插入事件。 首先,你需要连接到 mongodb 并获取一个指向数据库的指针: ```go client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017")) if err != nil { log.Fatal(err) } db := client.Database("mydb") ``` 然后,你可以使用 `db.Collection.Watch` 方法来获取一个 `ChangeStream`,并设置过滤器来只监听插入操作: ```go pipeline := mongo.Pipeline{ {{"$match", bson.D{{"operationType", "insert"}}}}, } cursor, err := db.Collection("mycoll").Watch(context.TODO(), pipeline) if err != nil { log.Fatal(err) } defer cursor.Close(context.TODO()) for cursor.Next(context.TODO()) { // 这里是循环体,每次插入都会执行一次 var event bson.M if err := cursor.Decode(&event); err != nil { log.Fatal(err) } // 获取插入数据 insertedData := event["fullDocument"].(bson.M) ownerID := insertedData["OwnerID"] name := insertedData["name"] quotaSize := insertedData["QuotaSize"] } ``` 在这个循环体中,每次插入操作都会触发执行一次。可以使用 `event["fullDocument"]` 获取插入的数据,然后使用键名获取 `OwnerID`、`name` 和 `QuotaSize` 字段的值。 注意: - 这段代码假定 mongodb 已经安装并运行,并且有一个名为 `mydb` 的数据库和一个名为 `mycoll` 的集合。 - 你需要