登录
首页 >  Golang >  Go问答

将 MongoDB _id 映射到 Golang 中的两个不同的结构字段

来源:stackoverflow

时间:2024-03-12 10:24:26 131浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《将 MongoDB _id 映射到 Golang 中的两个不同的结构字段》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我正在开发一个结合使用 go 和 mongodb 的项目。我被困在一个有如下结构的地方:

type booking struct {
    // booking fields
    id                          int                 `json:"_id,omitempty" bson:"_id,omitempty"`
    uid                         int                 `json:"uid,omitempty" bson:"uid,omitempty"`
    industryid                  int                 `json:"industry_id,omitempty" bson:"industry_id,omitempty"`
    locationid                  int                 `json:"location_id,omitempty" bson:"location_id,omitempty"`
    baselocationid              int                 `json:"base_location_id,omitempty" bson:"base_location_id,omitempty"`
    }

在此结构体中,字段 idint 类型。但我们知道mongodb的默认id是bsonobject类型。有时,系统会在 id 字段中生成默认的 mongodb id。

为了克服这个问题,我修改了结构,如下所示:

type booking struct {
        // booking fields
        id                          int                 `json:"_id,omitempty" bson:"_id,omitempty"`
        bsonid              bson.objectid       `json:"bson_id" bson:"_id,omitempty"`
        uid                         int                 `json:"uid,omitempty" bson:"uid,omitempty"`
        industryid                  int                 `json:"industry_id,omitempty" bson:"industry_id,omitempty"`
        locationid                  int                 `json:"location_id,omitempty" bson:"location_id,omitempty"`
        baselocationid              int                 `json:"base_location_id,omitempty" bson:"base_location_id,omitempty"`
        }

在上面的结构中,我将相同的 _id 字段映射到两个不同的结构字段 id (类型 int)和 bsonid (类型 bson.objectid)中。我希望如果整数类型的 id 出现,则它映射到 id 下,否则映射到 bsonid 下。

但这件事给出了以下错误:

duplicated key '_id' in struct models.booking

如何使用 go structs 实现这种类型的事情??

更新:

这是我为自定义编组/解组编写的代码:

func (booking *Booking) SetBSON(raw bson.Raw) (err error) {
    type bsonBooking Booking
    if err = raw.Unmarshal((*bsonBooking)(booking)); err != nil {
        return
    }
    booking.BsonId, err = booking.Id
    return
}

func (booking *Booking) GetBSON() (interface{}, error) {
    booking.Id = Booking.BsonId
    type bsonBooking *Booking
    return bsonBooking(booking), nil
}

但这会导致 id 和 bsonid 字段出现类型不匹配错误。我现在应该做什么?


解决方案


在您的原始结构中,您将此标记用于 id 字段:

bson:"_id,omitempty"

这意味着如果 id 字段的值为 0 (对于 int 类型为 zero value),则该字段将不会发送到 mongodb。但 _id 属性在 mongodb 中是强制的,因此在这种情况下,mongodb 服务器将为它生成一个 objectid

要克服这个问题,最简单的方法是确保 id 始终不为零;或者,如果 0 是有效 id,请从标记中删除 omitempty 选项。

如果您的集合必须允许混合类型的 id(intobjectid),那么最简单的方法是使用 interface{} 类型定义 id 字段,以便它可以容纳两种(实际上是所有)类型的键值:

id interface{} `json:"_id,omitempty" bson:"_id,omitempty"`

是的,使用这个可能会有点麻烦(例如,如果您明确需要 id 作为 int,则需要使用 type assertion),但这将解决您的问题。

如果您确实需要 2 个 id 字段,一个具有 int 类型,另一个具有 objectid 类型,那么您唯一的选择就是实现自定义 bson 编组和解组。这基本上意味着在您的结构类型上实现 bson.Getter 和/或 bson.Setter 接口(每个接口一个方法),您可以在其中执行任何您喜欢的操作来填充您的结构或组装要实际保存/插入的数据。有关详细信息和示例,请参阅Accessing MongoDB from Go

以下是使用自定义封送处理的示例:

从编组中删除 idbsonid 字段(使用 bson:"-" 标记),并添加第三个“临时”id 字段:

type booking struct {
        id     int           `bson:"-"`
        bsonid bson.objectid `bson:"-"`
        tempid interface{}   `bson:"_id"`
        // rest of your fields...
}

因此,无论您的 mongodb 中有什么 id,它最终都会出现在 tempid 中,并且只有这个 id 字段才会被发送并保存在 mongodb 的 _id 属性中。

在保存/插入结构值之前,使用 getbson() 方法从其他 id 字段(以设置的为准)设置 tempid ,并使用 setbson() 方法将 tempid 的值“复制”到另一个 id 字段之一从 mongodb 检索文档后,基于其动态类型的 id 字段:

func (b *Booking) GetBSON() (interface{}, error) {
    if b.Id != 0 {
        b.TempId = b.Id
    } else {
        b.TempId = b.BsonId
    }
    return b, nil
}

func (b *Booking) SetBSON(raw bson.Raw) (err error) {
    if err = raw.Unmarshal(b); err != nil {
        return
    }
    if intId, ok := b.TempId.(int); ok {
        b.Id = intId
    } else bsonId, ok := b.TempId.(bson.ObjectId); ok {
        b.BsonId = bsonId
    } else {
        err = errors.New("invalid or missing id")
    }

    return
}

注意:如果您不喜欢 booking 结构中的 tempid 字段,您可以创建 booking 的副本(例如 tempbooking),并且仅将 tempid 添加到其中,并使用 tempbookingzqbendczq b 用于编组/解组。您可以使用嵌入(tempbooking 可以嵌入 booking),这样您甚至可以避免重复。

本篇关于《将 MongoDB _id 映射到 Golang 中的两个不同的结构字段》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

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