登录
首页 >  Golang >  Go问答

如何使用 go mongo-driver 在 MongoDB 中将字节存储为二进制

来源:stackoverflow

时间:2024-02-20 12:06:24 386浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《如何使用 go mongo-driver 在 MongoDB 中将字节存储为二进制》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

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

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

type hash [20]byte

我尝试实现 valuemarshaler 接口,如下所示:

func (h hash) marshalbsonvalue() (bsontype.type, []byte, error) {
    return bsontype.binary, h[:20], nil
}

但很明显,h[:20] 不会为 mongo 返回有效字节。它会出现恐慌并返回以下内容:

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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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)
}

到这里,我们也就讲完了《如何使用 go mongo-driver 在 MongoDB 中将字节存储为二进制》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>