在这个例子中,我将存储混合数组


测试字符串:= '[[1,"a","b",2,"000000",[[1,2,3],[1,2,3]],"\"x","[y","'z",[[1,2,3],[1,2,3]]]]'


在 mongodb 中作为 json:


{datum: [[1,"a","b",2,"000000",[[1,2,3],[1,2,3]],"\"x","[y","'z",[[1,2,3],[1,2,3]]]]}


package main


import (

    "strings"

    "context"

    "encoding/json"

    "fmt"

    "log"

    "net/http"

    

    "go.mongodb.org/mongo-driver/bson"

    "go.mongodb.org/mongo-driver/bson/primitive"

)




type datum2 struct {

    Datum interface{} `json:datum`

}






var userCollection = db().Database("goTest").Collection("users") // get collection "users" from db() which returns *mongo.Client


func typeinterface2mongo() {

    

    

    var datum2 datum2_instance

    var interfacevalue []interface{}

    

    test_string := `[[1,"a","b",2,"000000",[[1,2,3],[1,2,3]],"\"x","[y","'z",[[1,2,3],[1,2,3]]]]`

    if err := json.Unmarshal([]byte(test_string), &interfacevalue); err != nil {

        fmt.Println(err)

        return

    }

    

    fmt.Println(test_string)

    fmt.Println(interfacevalue)

    datum2_instance.Datum=interfacevalue

    userCollection.InsertOne(context.TODO(), datum2_instance)

    fmt.Println(datum2_instance)

    fmt.Println(datum2_instance.Datum)  

}