tencent cloud

All product documents
TencentDB for MongoDB
Go Connection Sample
Last updated: 2024-01-15 14:49:56
Go Connection Sample
Last updated: 2024-01-15 14:49:56

Notes

TencentDB for MongoDB provides two usernames rwuser and mongouser by default to support the MONGODB-CR and SCRAM-SHA-1 authentication methods, respectively. The connecting URIs for the two authentication methods are formed differently. For more information, please see Connecting to TencentDB for MongoDB Instance.

Sample Code for mgo

func GetMgoURL(ip, user, password string, port int) string {
urlString := ""
if user == "" && password == "" {
urlString = fmt.Sprintf("mongodb://%s:%d/admin", ip, port)
}else {
urlString = fmt.Sprintf("mongodb://%s:%s@%s:%d/admin", url.QueryEscape(user), url.QueryEscape(password), ip, port)
}

return urlString
}


url := service.GetMgoURL(reqPara.Ip, reqPara.User, reqPara.Password, reqPara.Port)
session, err := mgo.Dial(url)

Sample Code for MongoDB Go

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support