我写了一个更新函数,但是多次执行会报错context deadline exceeded。
我的功能:
func Update(link string, m bson.M) {
configInfo := config.Config()
// client := GetInstance().client
// ctx := GetInstance().ctx
client, _ := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := client.Connect(ctx)
if err != nil {
fmt.Print("connect error!")
fmt.Println(err)
}
db := client.Database("test")
lianjia := db.Collection("test")
_, err = lianjia.UpdateOne(ctx, bson.M{"Link": link}, bson.M{"$set": m})
if err != nil {
fmt.Print("update error!")
fmt.Println(err)
}
}
输出:
update error!context deadline exceeded
慕仙森
不负相思意
慕雪6442864
智慧大石
吃鸡游戏
相关分类