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 “ v1.10.1”. The connection string is similar to this:


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(
if err != nil {

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

log.Println("Mongodb Connect!!")

database := client.Database(databaseName)

Can you help me to solve this problem?