我有一个结构体,其中引用了*big.Int。当我将这个结构体简单地存储到MongoDB中(使用官方驱动程序)时,当将结构体取回时,该字段变为nil。存储big.Int到MongoDB的正确/最佳方法是什么?
type MyStruct struct {
Number *big.Int
}
nb := MyStruct{Number: big.NewInt(42)}
r, _ := db.Collection("test").InsertOne(context.TODO(), nb)
result := &MyStruct{}
db.Collection("test").FindOne(context.TODO(), bson.D{{"_id", r.InsertedID}}).Decode(result)
fmt.Println(result) // <== Number will be 0 here
到目前为止,我最好的想法是创建一个big.Int的包装器,实现MarshalBSON
和UnmarshalBSON
(说实话,我甚至不确定如何正确地实现它)。但这将非常不方便。
big.Int
转换为string
,并使用 MarshalBSON? - soulmachine