[GODRIVER-451] Why runCommand execute in the Primary Created: 06/Jun/18  Updated: 11/Sep/19  Resolved: 08/Oct/18

Status: Closed
Project: Go Driver
Component/s: CRUD
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: GuoqiangXu Assignee: Kristofer Brandow (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to GODRIVER-561 Database.RunCommand should use the pr... Closed

 Description   

I saw this code:

 

// mongo-go-driver/mongo/database.go
 
func (db *Database) RunCommand(ctx context.Context, runCommand interface{}) (bson.Reader, error) {
	if ctx == nil {		ctx = context.Background()	}
	cmd := command.Command{DB: db.Name(), Command: runCommand}	return dispatch.Command(ctx, cmd, db.client.topology, db.writeSelector)}

Why default use db.writeSelector.

 

 

I want connect to Secondary node and get data from db.serverStatus(), but always get data from Primary.

Example:

 

 
if rs, err := db.RunCommand(context.Background(), bson.NewDocument(bson.EC.Int32("serverStatus", 1))); err != nil{
   fmt.Printf("Error %s", err)
}else {
   if elment, err := rs.Lookup("host"); err != nil{
      fmt.Printf("Error %s", err)
   }else{
      fmt.Println(elment.Value().StringValue())
   }
}

 

 



 Comments   
Comment by Kristofer Brandow (Inactive) [ 17/Sep/18 ]

Hi xjxyxgq,

We have logged GODRIVER-561, to handle properly selecting a server based on read preference. That said, if you are attempting to call server status on a specific secondary node, you should put connect=direct in the connection string used to create the *mongo.Client.

Thanks,

Kris

Generated at Thu Feb 08 08:34:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.