I need help with mongo atlas service

First I gonna apologize with my bad English.

So Im using golang with mongo db ( mongo atlas service )

for my connection my code is look like this

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	clientOptions := options.Client().ApplyURI(URL)

So now we face issue about query and not timeout for 700k ms

Hey @management_zabtech, thanks for the question! Operation timeout is controlled by the Context passed to an operation function. The Context passed to mongo.Connect has no effect on operation timeout.

Here’s an example that times out an Insert operation after 10 seconds:

func main() {
	opts := options.Client().ApplyURI(URL)
	client, err := mongo.Connect(context.Background(), opts)
	if err != nil {
		panic(err)
	}
	coll := client.Database("test").Collection("test")

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	_, err = coll.InsertOne(ctx, bson.D{{"key", "value"}})
	if err != nil {
		panic(err)
	}
}

Where are you using the Context you create in your example code? Is it in mongo.Connect or in an operation function?

1 Like

Thx sir , I got advise by Create index in performance advisor menu , after created everything is fine