You need to enable JavaScript to run this app.
Document Database for MongoDB

Document Database for MongoDB

Copy page
Download PDF
Go SDK
Code examples
Copy page
Download PDF
Code examples
This topic provides sample code for the MongoDB Go SDK for your reference.​
Prerequisite​
Configure request timeout​
When using the Go SDK to call MongoDB interfaces, there is no default timeout.​
You can specify the established timeout for client requests through WithHTTPClient(&http.Client{Timeout: ${timeout} * time.Second}) when initializing the client. If the request is not completed within this time, it will be terminated with a timeout error reported.​
For example, you can use the following code to set the client's request timeout to 10 seconds.​
​
config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).​
WithRegion(region).WithHTTPClient(&http.Client{Timeout: 10 * time.Second})​
​
Creating a MongoDB instance​
tip​
For the definitions, value ranges, and other information of parameters used in the following SDK code, refer to CreateDBInstance.​
​
Example 1
Example 2
​
Create a sharding cluster instance with the sample code below and configure it as follows:​
  • The availability zone where the instance is located: China (Beijing) - availability zone A.​
  • Storage capacity: 100 GiB.​
  • Node specifications:​
  • Include 2 Mongos nodes, each with 2 cores 4 GiB.​
  • Include 2 shards (each includes 3 nodes by default), and the specifications of the nodes in each shard is 2 cores 4 GiB.​
​
package main​
​
import (​
"fmt"​
"os"​
​
"github.com/volcengine/volcengine-go-sdk/service/mongodb"​
"github.com/volcengine/volcengine-go-sdk/volcengine"​
"github.com/volcengine/volcengine-go-sdk/volcengine/credentials"​
"github.com/volcengine/volcengine-go-sdk/volcengine/session"​
)​
func main() {​
var (​
ak string​
sk string​
region string​
config *volcengine.Config​
sess *session.Session​
client *mongodb.MONGODB​
resp *mongodb.CreateDBInstanceOutput​
err error​
)​
​
ak = "Your AK"​
sk = "Your SK"​
region = "Your Region"​
config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).​
WithRegion(region)​
​
sess, err = session.NewSession(config)​
if err != nil {​
fmt.Printf("Failed to create session, err: %v\n", err)​
os.Exit(1)​
}​
​
client = mongodb.New(sess)​
​
// Set the configuration information needed for creating an instance​
zoneId := "cn-beijing-a"​
nodeSpec := "mongo.shard.2c4g"​
storageSpaceGb := 100​
vpcId := "vpc-13f24t16guqdc3n6nu4k5****"​
subnetId := "subnet-rr96lovl7f28v0x58tv****"​
instanceName := "go_sdk_test_shard"​
mongosNodeSpec := "mongo.mongos.2c4g"​
instanceType := "ShardedCluster"​
shardNumber := 2​
mongosNodeNumber := 2​
​
​
// Call the CreateDBInstance API to create an instance​
resp, err = client.CreateDBInstance(&mongodb.CreateDBInstanceInput{​
ZoneId: &zoneId,​
NodeSpec: &nodeSpec,​
StorageSpaceGB: &storageSpaceGb,​
VpcId: &vpcId, ​
SubnetId: &subnetId,​
InstanceName: &instanceName,​
MongosNodeSpec: &mongosNodeSpec,​
InstanceType: &instanceType,​
shardNumber: &shardNumber,​
MongosNodeNumber: &mongosNodeNumber,​
})​
​
if err != nil {​
fmt.Printf("Error when calling MONGODBApi->CreateDBInstance, err: %v\n", err)​
os.Exit(1)​
}​
​
fmt.Println(resp)​
}​
​
​
Querying MongoDB instances​
tip​
For the definitions, value ranges, and other information of parameters used in the following SDK requests, refer to DescribeDBInstanceDetail.​
​
package main​
​
import (​
"fmt"​
"os"​
​
"github.com/volcengine/volcengine-go-sdk/service/mongodb"​
"github.com/volcengine/volcengine-go-sdk/volcengine"​
"github.com/volcengine/volcengine-go-sdk/volcengine/credentials"​
"github.com/volcengine/volcengine-go-sdk/volcengine/session"​
)​
​
func main() {​
var (​
ak string​
sk string​
region string​
config *volcengine.Config​
sess *session.Session​
client *mongodb.MONGODB​
resp *mongodb.DescribeDBInstanceDetailOutput​
err error​
)​
​
ak = "Your AK"​
sk = "Your SK"​
region = "Your Region"​
config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).​
WithRegion(region)​
​
sess, err = session.NewSession(config)​
if err != nil {​
fmt.Printf("Failed to create session, err: %v\n", err)​
os.Exit(1)​
}​
​
client = mongodb.New(sess)​
​
// Specify the instance ID need to view​
instanceId := "mongo-replica-e649ae42****"​
​
// Call the DescribeDBInstanceDetail API to view the information about the instance​
resp, err = client.DescribeDBInstanceDetail(&mongodb.DescribeDBInstanceDetailInput{​
InstanceId: &instanceId,​
})​
​
if err != nil {​
fmt.Printf("Error when calling MONGODBApi->DescribeDBInstanceDetail, err: %v\n", err)​
os.Exit(1)​
}​
​
fmt.Println(resp)​
}​
​
​
Last updated: 2026.04.10 14:24:01