You need to enable JavaScript to run this app.
导航
代码示例
最近更新时间:2024.03.29 10:07:11首次发布时间:2022.10.19 17:37:37

本文提供 MongoDB Go SDK 的代码示例供您参考。

前提条件

  1. 下载安装 SDK
  2. 初始化客户端

设置请求超时时间

使用 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
    使用如下示例代码创建 1 个分片集群实例,配置如下:

    • 实例所在可用区:华北 2(北京)- 可用区 A
    • 存储空间:100GiB
    • 节点规格:
      • 包含 2 个 Mongos 节点,规格均为 1 核 2GiB
      • 包含 2 个 Shard 分片(每个分片默认包含 3 个节点),每个 Shard 中的节点规格均为 2 核 4GiB
    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)
    
       // 设置需要创建实例的配置信息
       zoneId := "cn-beijing-a"
       nodeSpec := "mongo.shard.2c4g"
       storageSpaceGb := 100
       vpcId := "vpc-13f24t16guqdc3n6nu4k5****"
       subnetId := "subnet-rr96lovl7f28v0x58tv****"
       instanceName := "go_sdk_test_shard"
       mongosNodeSpec := "mongo.mongos.1c2g"
       instanceType := "ShardedCluster"
       shardNumber := 2
       mongosNodeNumber := 2
          
    
       // 调用 CreateDBInstance 接口创建实例
       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)
    }
    
  • 示例 2
    使用如下示例代码创建 1 个副本集实例(默认包含 3 个节点),配置如下:

    • 实例所在可用区:华北 2(北京)- 可用区 A
    • 存储空间:100GiB
    • 节点规格:1 核 2GiB
    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)
    
       // 设置需要创建实例的配置信息
       vpcId := "vpc-13f24t16guqdc3n6nu4k5****"
       zoneId := "cn-beijing-a"
       nodeSpec := "mongo.1c2g"
       storageSpaceGb := 100
       subnetId := "subnet-rr96lovl7f28v0x58tv****"
       instanceName := "go_sdk_test_replica"
    
       // 调用 CreateDBInstance 接口创建实例
       resp, err = client.CreateDBInstance(&mongodb.CreateDBInstanceInput{
           VpcId:              &vpcId,
           ZoneIds:            &zoneIds,
           NodeSpec:           &nodeSpec,
           StorageSpaceGB:     &storageSpaceGb,
           SubnetId:           &subnetId,
           InstanceName:       &instanceName,
       })
    
       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)

   // 指定要查看的实例 ID
   instanceId := "mongo-replica-e649ae42****"

   // 调用 DescribeDBInstanceDetail 接口查看实例信息
   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)
}