无法将类型primitive.ObjectID设置为ObjectID。

huangapple go评论103阅读模式
英文:

cannot set type primitive.ObjectID to ObjectID

问题

在使用docstore集合迭代器将MongoDB文档绑定到GO结构体时,遇到了以下错误。

错误详情:

Cosmos DB构造函数代码

    client, err := c.newClient(ctx, true)
    if err != nil {
        log.Error("error connecting to mongodb cluster", zap.Error(err))
        return nil, err
    }

    database := client.Database(c.dbName)
    collection := database.Collection(c.collName)

    return mongodocstore.OpenCollection(collection, "", nil)

用于与MongoDB文档映射的GO结构体

type utterance struct {
    ID               primitive.ObjectID `docstore:"_id,omitempty"`
    User             string             `docstore:"user,omitempty"`
    Locale           string             `docstore:"Locale,omitempty"`
    Text             string             `docstore:"Text,omitempty"`
    Source           string             `docstore:"Source,omitempty"`
    Timestamp        time.Time          `docstore:"Timestamp,omitempty"`
    DocstoreRevision interface{}
}

MongoDB文档

{
    "_id" : ObjectId("60d5e18539864e948a8851a6"),
    "User" : "auth0|6049b5ef5d79540071db6a0a",
    "Locale" : "en_US",
    "Text" : "Hi",
    "Source" : "UTTERANCE_SOURCE_USER",
    "Timestamp" : {
        "$date" : 1624629637002
    },
    "DocstoreRevision" : "bf3b35d8-54ed-4a23-a08f-7d41b5c34085"
}

调用docstore集合并进行迭代的方法

i := s.collection.Query().Get(ctx)
defer i.Stop()

var results []*services.Utterance

for {
    fmt.Println("for every document:")
    doc := &utterance{}
    err := i.Next(ctx, doc) // 在这一行出现错误
    if err == io.EOF {
        break
    } else if err != nil {
        fmt.Println("getting to this err block")
        return nil, err
    }

    u := doc.ToProto()
    results = append(results, u)
}
英文:

Getting this error while binding mongodb document to GO Struct using docstore collection iterator.

Database: Azure CosmosDB mongoDb API
Go Driver: Docstore

Code details:

Cosmos DB Constructors Code

    client, err := c.newClient(ctx, true)
    if err != nil {
        log.Error("error connecting to mongodb cluster", zap.Error(err))
        return nil, err
    }

    database := client.Database(c.dbName)
    collection := database.Collection(c.collName)

    return mongodocstore.OpenCollection(collection, "", nil)

Go struct for mapping with Mongo Db document

type utterance struct {
    ID               primitive.ObjectID `docstore:"_id,omitempty"`
    User             string             `docstore:"user,omitempty"`
    Locale           string             `docstore:"Locale,omitempty"`
    Text             string             `docstore:"Text,omitempty"`
    Source           string             `docstore:"Source,omitempty"`
    Timestamp        time.Time          `docstore:"Timestamp,omitempty"`
    DocstoreRevision interface{}
}

MondoDb Document

{
    "_id" : ObjectId("60d5e18539864e948a8851a6"),
    "User" : "auth0|6049b5ef5d79540071db6a0a",
    "Locale" : "en_US",
    "Text" : "Hi",
    "Source" : "UTTERANCE_SOURCE_USER",
    "Timestamp" : {
        "$date" : 1624629637002
    },
    "DocstoreRevision" : "bf3b35d8-54ed-4a23-a08f-7d41b5c34085"
}

Method to call the docstore collection and iterate

i := s.collection.Query().Get(ctx)
    defer i.Stop()

    var results []*services.Utterance

    for {
        fmt.Println("for every document: ")
        //var native utterance
        doc := &utterance{}
        err := i.Next(ctx, doc) //error at this line
        if err == io.EOF {
            break
        } else if err != nil {
            fmt.Println(“getting to this err block“)
            return nil, err
        }

        u := doc.ToProto()
        results = append(results, u)
    }

答案1

得分: 1

尝试将ID设置为指针类型:

type utterance struct {
    ID               *primitive.ObjectID `docstore:"_id,omitempty"`
    User             string             `docstore:"user,omitempty"`
    Locale           string             `docstore:"Locale,omitempty"`
    Text             string             `docstore:"Text,omitempty"`
    Source           string             `docstore:"Source,omitempty"`
    Timestamp        time.Time          `docstore:"Timestamp,omitempty"`
    DocstoreRevision interface{}
}

可能是因为在创建要解码的结构体时,primitive.ObjectID被初始化了,而驱动程序不知道如何解码它。

请注意,docstore是一个通用的文档驱动程序,不特定于MongoDB。因此,它可能无法很好地处理MongoDB特定的类型。


阅读mongodb驱动程序的文档:https://pkg.go.dev/gocloud.dev/docstore/mongodocstore

有一种方法可以设置存储在_id字段中的内容。我理解docstore不知道如何从MongoDB解码ObjectID

英文:

Try setting the ID to a pointer type:

type utterance struct {
    ID               *primitive.ObjectID `docstore:"_id,omitempty"`
    User             string             `docstore:"user,omitempty"`
    Locale           string             `docstore:"Locale,omitempty"`
    Text             string             `docstore:"Text,omitempty"`
    Source           string             `docstore:"Source,omitempty"`
    Timestamp        time.Time          `docstore:"Timestamp,omitempty"`
    DocstoreRevision interface{}
}

Probably when you create the struct to be decoded the primitive.ObjectID is being initialized and the driver don't know to decode it.

And be careful. The docstore is a generic Document driver. It is not specific for MongoDB. So it probably don't work well with MongoDB specific types.


Reading the driver for mongodb https://pkg.go.dev/gocloud.dev/docstore/mongodocstore

There is a way to set what is stored in the _id field. I understand docstore don't know how to decode ObjectID from MongoDB.

huangapple
  • 本文由 发表于 2021年6月28日 22:18:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/68165002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定