You need to enable JavaScript to run this app.
文档中心
文档数据库 MongoDB 版

文档数据库 MongoDB 版

复制全文
下载 pdf
Go SDK
代码示例
复制全文
下载 pdf
代码示例
本文提供 MongoDB Go SDK 的代码示例供您参考。​
前提条件​
设置请求超时时间​
使用 Go SDK 调用 MongoDB 接口时,默认无超时时间设置。​
您可以在初始化客户端时,通过 WithHTTPClient(&http.Client{Timeout: ${超时时间} * time.Second}) 来指定客户端的请求超时时间,若未在该时间内完成,请求即会终止并报超时错误。​
例如,您可以通过如下代码将客户端的请求超时时间设置为 10 秒。​
​
config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).​
WithRegion(region).WithHTTPClient(&http.Client{Timeout: 10 * time.Second})​
​
创建 MongoDB 实例​
说明​
关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 CreateDBInstance。​
​
示例 1
示例 2
​
使用如下示例代码创建 1 个分片集群实例,配置如下:​
  • 实例所在可用区:华北 2(北京)- 可用区 A。​
  • 存储空间:100 GiB。​
  • 节点规格:​
  • 包含 2 个 Mongos 节点,规格均为 2 核 4 GiB。​
  • 包含 2 个 Shard 分片(每个分片默认包含 3 个节点),每个 Shard 中的节点规格均为 2 核 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)​
}​
​
​
查询 MongoDB 实例信息​
说明​
关于下述 SDK 请求中所使用参数的定义、取值范围等信息,请参见 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)​
}​
​
​
最近更新时间:2026.04.10 14:24:18
这个页面对您有帮助吗?
有用
有用
无用
无用