mongodb atlas cluster golang insert struct slice 接口上下文取消,当前拓扑

mongodb atlas cluster golang insert struct slice interface context canceled, current topology

当我将一组文档插入 MongoDB Atlas 集合时,我收到以下错误消息:

2021/12/23 09:37:03 服务器选择错误:上下文已取消,当前拓扑:{ 类型:ReplicaSetNoPrimary,服务器:[{ 地址:cluster-attitude-shard-00-00 .o7pjk.mongodb.net:27017,类型:未知},{地址:cluster-attitude-shard-00-01。o7pjk.mongodb.net:27017,类型:未知},{地址:cluster-attitude-shard-00 -02.o7pjk.mongodb.net:27017, 类型: 未知 }, ] }

我使用下一个代码:

interfaz_slice := ToInterfaceSlice(students)

_, err := coleccion.InsertMany(ctx, interfaz_slice)

函数“ToInterfaceSlice”接收结构切片和return接口切片

我不明白我哪里弄错了

提前致谢

问题的新部分:

文件片段“main.go”:

func main() {

    var students []data.TypeStudent

    absPath, _ := filepath.Abs("data/students.csv")

    students = data.LeerFichero(absPath)

    data.ConectaBD()

    data.InsertaColleccionEstudiantes(students)

}

文件片段“students.go”:

type TypeStudent struct {
    FirstName string `bson:"first_name" json:"first_name"`
    LastName  string `bson:"last_name" json:"last_name"`
    Class     string `bson:"class" json:"class"`
}

func ToInterfaceSlice(lista []TypeStudent) []interface{} {
    iface := make([]interface{}, len(lista))
    for i := range lista {
        iface[i] = lista[i]
    }
    return iface
}

文件片段“basedatos.go”:

func ConectaBD() {

    cliente_local, err := mongo.NewClient(options.Client().ApplyURI(cadena_conexion))
    if err != nil {
        log.Fatal(err)
    }
    ctx, cancelar = context.WithTimeout(context.Background(), 10*time.Second)
    err = cliente_local.Connect(ctx)
    if err != nil {
        log.Fatal(err)
    }
    defer cancelar()

    mongo_cliente = cliente_local.Database("attitude")

    log.Println("[+]Connected to MongoDB Atlas")

}

func InsertaColleccionEstudiantes(students []TypeStudent) {

    coleccion = mongo_cliente.Collection("students")

    interfaz_slice := ToInterfaceSlice(students)

    log.Println("[+]A slice of interfaces are inserted directly into MONGODB")

    _, err := coleccion.InsertMany(ctx, interfaz_slice)

    if err != nil {
        log.Fatal(err)
    }

}

信息不充分。检查 IP 是否在白名单中。还要指定您正在使用的上下文。你可以试试 context.TODO().

我试过了,效果很好。

确保您的连接成功。同时使用 defer.

关闭连接

这是您可以尝试使用的示例代码:

client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
    if err != nil {
        panic(err)
    }
    defer func() {
        if err = client.Disconnect(context.TODO()); err != nil {
            panic(err)
        }
    }()

    // begin insertMany
    coll := client.Database("insertDB").Collection("haikus")
    docs := []interface{}{
        bson.D{{"title", "Record of a Shriveled Datum"}, {"text", "No bytes, no problem. Just insert a document, in MongoDB"}},
        bson.D{{"title", "Showcasing a Blossoming Binary"}, {"text", "Binary data, safely stored with GridFS. Bucket the data"}},
    }

    result, err := coll.InsertMany(context.TODO(), docs)
    if err != nil {
        panic(err)
    }

为了更好地理解,请参阅此 link:https://raw.githubusercontent.com/mongodb/docs-golang/master/source/includes/usage-examples/code-snippets/insertMany.go