Problem with transactions

I am using a dedicated mongo server on atlas M10. The language I’m using is Go(golang) in version 1.18.
The mongo driver is “go.mongodb.org/mongo-driver v1.10.1”. The connection string is similar to this:

mongodb+srv://:@.mongodb.net/?retryWrites=true&w=majority

Sometimes I’m getting an error, but it’s not clear what the problem is:

(NotWritablePrimary) Not primary so we cannot begin or continue a transaction

It happens when I run this command:

filter := bson.D{{"_id", view.Id}}
result, err := collection.ReplaceOne(ctx, filter, model)

The connection is configured as:

client, err := mongo.Connect(
context.TODO(),
options.Client().ApplyURI(connURI),
)
if err != nil {
panic(err)
}

if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
panic(err)
}

log.Println("Mongodb Connect!!")

database := client.Database(databaseName)

Can you help me to solve this problem?