Home > Backend Development > Golang > How to Ignore Null Values When Unmarshalling MongoDB Documents in Go?

How to Ignore Null Values When Unmarshalling MongoDB Documents in Go?

Susan Sarandon
Release: 2025-01-02 16:23:07
Original
855 people have browsed it

How to Ignore Null Values When Unmarshalling MongoDB Documents in Go?

Ignoring Null Values during MongoDB Document Unmarshalling

When unmarshalling a MongoDB document into a Go struct that contains non-nullable string fields, encountering null values in the document can lead to errors. To address this issue, it is necessary to find a way to ignore these null values during unmarshalling.

Using a Custom Decoder

One approach to handling null values is by creating a custom decoder for the string type. This custom decoder would recognize null values and handle them by setting the corresponding field to an empty string, effectively ignoring the null value:

import (
    "go.mongodb.org/mongo-driver/bson/bsoncodec"
    "go.mongodb.org/mongo-driver/bson/bsonrw"
    "go.mongodb.org/mongo-driver/bson/bsontype"
)

type nullawareStrDecoder struct{}

func (nullawareStrDecoder) DecodeValue(dctx bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
    if !val.CanSet() || val.Kind() != reflect.String {
        return errors.New("bad type or not settable")
    }
    var str string
    var err error
    switch vr.Type() {
    case bsontype.String:
        if str, err = vr.ReadString(); err != nil {
            return err
        }
    case bsontype.Null:
        if err = vr.ReadNull(); err != nil {
            return err
        }
    default:
        return fmt.Errorf("cannot decode %v into a string type", vr.Type())
    }

    val.SetString(str)
    return nil
}
Copy after login

This custom decoder can then be registered with a bsoncodec.Registry and applied to a mongo.Client object:

clientOpts := options.Client().
    ApplyURI("mongodb://localhost:27017/").
    SetRegistry(
        bson.NewRegistryBuilder().
            RegisterDecoder(reflect.TypeOf(""), nullawareStrDecoder{}).
            Build(),
    )
client, err := mongo.Connect(ctx, clientOpts)
Copy after login

Creating a Type-Neutral Null-Aware Decoder

To handle null values for multiple types, it is possible to create a single, type-neutral decoder that checks for null values and, if encountered, sets the corresponding field to the zero value of its type:

type nullawareDecoder struct {
    defDecoder bsoncodec.ValueDecoder
    zeroValue  reflect.Value
}

func (d *nullawareDecoder) DecodeValue(dctx bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
    if vr.Type() != bsontype.Null {
        return d.defDecoder.DecodeValue(dctx, vr, val)
    }

    if !val.CanSet() {
        return errors.New("value not settable")
    }
    if err := vr.ReadNull(); err != nil {
        return err
    }

    val.Set(d.zeroValue)
    return nil
}
Copy after login

This decoder can be registered with a bsoncodec.Registry for specific types or for all types:

customValues := []interface{}{
    "",       // string
    int(0),   // int
    int32(0), // int32
}

rb := bson.NewRegistryBuilder()
for _, v := range customValues {
    t := reflect.TypeOf(v)
    defDecoder, err := bson.DefaultRegistry.LookupDecoder(t)
    if err != nil {
        panic(err)
    }
    rb.RegisterDecoder(t, &nullawareDecoder{defDecoder, reflect.Zero(t)})
}
Copy after login

The above is the detailed content of How to Ignore Null Values When Unmarshalling MongoDB Documents in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template