-
Type:
Bug
-
Resolution: Works as Designed
-
Priority:
Major - P3
-
None
-
Affects Version/s: 1.10.2
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
I found that although the primaryPreferred is specified, a small number of request will still be sent to the secondary node, even though the primary node is available at the time.
mongo-go-driver: v1.10.2
mongodb: 4.0 replicaset
```
var url = flag.String("url", "127.0.0.1:27031", "mongodb url")
func main() {
flag.Parse()
f := func() error {
clientOptions := options.Client().ApplyURI(*url).SetReadPreference(readpref.PrimaryPreferred())
client, err := mongo.NewClient(clientOptions)
if err != nil
if err = client.Connect(context.Background()); err != nil
defer client.Disconnect(context.Background())
/*
if err = client.Ping(context.Background(), readpref.Primary()); err != nil
else
{ fmt.Println("ping...") } */
coll := client.Database("mydb").Collection("coll")
filter := bson.D{{Key: "_id", Value: "test_val_readpref"}}
res := coll.FindOne(context.Background(), filter)
if res.Err() != nil
return nil
}
for i := 0; i < 1000; i++ {
if err := f(); err != nil
else
{ fmt.Println("success") } }
}
```
I have to ping the primary node before sending the request to make sure I got data from the primary.
From the documentation here: https://www.mongodb.com/docs/v5.0/core/read-preference-use-cases/
The description of primaryPreferred is that
```
operations read from the primary but if it is unavailable, operations read from secondary members in most situations.
```
When the primary node is available and should all operations read from primary members?