mongodb - 如何使用 go mongo-driver 在 MongoDB 中将 []byte 存储为二进制文件

标签 mongodb go mongo-go

我正在使用 using go.mongodb.org/mongo-driver/mongo我正在尝试在 MongoDB 中讲述字节 slice 。但是,驱动程序将其编码为整数数组。

就我而言,我有以下类型:

type Hash [20]byte

我试图实现 ValueMarshaler界面为傻瓜:

func (h Hash) MarshalBSONValue() (bsontype.Type, []byte, error) {
    return bsontype.Binary, h[:20], nil
}

但是很明显h[:20]不返回 mongo 的有效字节。它 panic 并返回以下内容:
panic: runtime error: slice bounds out of range [:-658764325]

goroutine 39 [running]:
go.mongodb.org/mongo-driver/x/bsonx/bsoncore.ReadElement(0xc0004bca1c, 0x1d, 0xe4, 0x55b6910, 0xffffffffffffff01, 0xc000178d00, 0xc00129aa18, 0x406d52d, 0x4819ea0, 0xc000bba240)
       /go/pkg/mod/go.mongodb.org/mongo-driver@v1.0.1/x/bsonx/bsoncore/bsoncore.go:122 +0x271
go.mongodb.org/mongo-driver/x/bsonx/bsoncore.Document.Validate(0xc0004bca18, 0x21, 0xe8, 0x4799100, 0x26)
       /go/pkg/mod/go.mongodb.org/mongo-driver@v1.0.1/x/bsonx/bsoncore/document.go:377 +0xe4
go.mongodb.org/mongo-driver/x/bsonx.(*Doc).UnmarshalBSON(0xc00129aba0, 0xc0004bca18, 0x21, 0xe8, 0x5, 0xc00129abc8)
       /go/pkg/mod/go.mongodb.org/mongo-driver@v1.0.1/x/bsonx/document.go:225 +0x6e
go.mongodb.org/mongo-driver/x/bsonx.ReadDoc(0xc0004bca18, 0x21, 0xe8, 0xc0004bca18, 0x21, 0xe8, 0xc0004bca39, 0x0)
       /go/pkg/mod/go.mongodb.org/mongo-driver@v1.0.1/x/bsonx/document.go:54 +0x77
go.mongodb.org/mongo-driver/x/bsonx.(*Val).UnmarshalBSONValue(0xc00129b2c0, 0x3, 0xc0004bca18, 0x21, 0xe8, 0x21, 0xe8)
       /go/pkg/mod/go.mongodb.org/mongo-driver@v1.0.1/x/bsonx/value.go:249 +0x90f
go.mongodb.org/mongo-driver/x/bsonx.(*Doc).UnmarshalBSON(0xc00129b370, 0xc0004bca0a, 0x30, 0xf6, 0x44747df, 0xc0000b21e0)
       /go/pkg/mod/go.mongodb.org/mongo-driver@v1.0.1/x/bsonx/document.go:236 +0x173
go.mongodb.org/mongo-driver/x/bsonx.ReadDoc(0xc0004bca0a, 0x30, 0xf6, 0xc0004bca0a, 0x30, 0xf6, 0xc0004bca3a, 0x0)
       /go/pkg/mod/go.mongodb.org/mongo-driver@v1.0.1/x/bsonx/document.go:54 +0x77
go.mongodb.org/mongo-driver/x/bsonx.(*Val).UnmarshalBSONValue(0xc00129ba90, 0x3, 0xc0004bca0a, 0x30, 0xf6, 0x30, 0xf6)
       /go/pkg/mod/go.mongodb.org/mongo-driver@v1.0.1/x/bsonx/value.go:249 +0x90f
go.mongodb.org/mongo-driver/x/bsonx.(*Doc).UnmarshalBSON(0xc00129bb40, 0xc0004bca00, 0x3b, 0x100, 0xc0000b21e0, 0x100)
       /go/pkg/mod/go.mongodb.org/mongo-driver@v1.0.1/x/bsonx/document.go:236 +0x173
go.mongodb.org/mongo-driver/x/bsonx.ReadDoc(0xc0004bca00, 0x3b, 0x100, 0x0, 0x100, 0x4833880, 0xc000bba090, 0xc0004bca00)
       /go/pkg/mod/go.mongodb.org/mongo-driver@v1.0.1/x/bsonx/document.go:54 +0x77
go.mongodb.org/mongo-driver/mongo.transformDocument(0xc00014a070, 0x4833880, 0xc000bba090, 0xc000bba120, 0x1, 0x1, 0x0, 0x0)
       /go/pkg/mod/go.mongodb.org/mongo-driver@v1.0.1/mongo/mongo.go:155 +0x1f5
go.mongodb.org/mongo-driver/mongo.(*Collection).UpdateOne(0xc00010e1e0, 0x4cca0a0, 0xc00010e3c0, 0x4833880, 0xc000bba060, 0x4833880, 0xc000bba090, 0x0, 0x0, 0x0, ...)
       /go/pkg/mod/go.mongodb.org/mongo-driver@v1.0.1/mongo/collection.go:530 +0xf0


Process finished with exit code 2

最佳答案

BSON 是二进制 JSON。 JSON 本身没有字节数组类型,但它确实有数组。

该库可以处理类型而无需任何额外的编码器。

这是一个最小的工作示例。

package main

import (
    "context"
    "fmt"
    "time"

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

type Hash [4]byte

type Model struct {
    Key Hash `bson:"key"`
}

func main() {
    client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
    if err != nil {
        panic(err)
    }
    ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
    defer cancel()

    err = client.Connect(ctx)
    if err != nil {
        panic(err)
    }

    db := client.Database("database")
    collection := db.Collection("collection")

    // insert as plain []byte slice
    _, err = collection.InsertOne(ctx, bson.M{"key": [4]byte{1, 2, 3, 4}})
    if err != nil {
        panic(err)
    }

    // insert h as a Hash type
    h := Hash([4]byte{1, 2, 3, 4})
    _, err = collection.InsertOne(ctx, bson.M{"key": h})
    if err != nil {
        panic(err)
    }

    cursor, err := collection.Find(ctx, bson.M{})
    if err != nil {
        panic(err)
    }

    var all []Model
    if err := cursor.All(ctx, &all); err != nil {
        panic(err)
    }

    fmt.Println(all)
}

关于mongodb - 如何使用 go mongo-driver 在 MongoDB 中将 []byte 存储为二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59507580/

相关文章:

javascript - 如何从嵌套数组中查找数据?

go - 如何获取大文件的 byte slice ?

go - 读取 Go gctrace 输出

go - 如果资源是结构的成员,则何时以及如何关闭资源

mongodb - 由于超时,UpdateOne 在客户端失败,但 MongoDB 仍然处理它

mongodb - 将字符串 slice 转换为 BSON 数组

json - 将一个 mongodb 对象数组转储到一个结构片段中

mongodb - Mongo - rs.initiate() 错误代码 76

mongodb - Meteor 确实看到了一个带有 MongoInternals.RemoteCollectionDriver 的远程 mongodb 实例

mongodb - 如何在 Kubernetes 中使用外部 MongoDB 服务