I wrote an update function, but multiple executions will give the error context deadline exceeded
.
My function:
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)
}
}
The output:
update error!context deadline exceeded
Change mongodb://localhost:27017
to mongodb://127.0.0.1:27017/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With