Using mongodb go driver for decoding documents into structs with custom type fields

16,002

Solution 1

Foreword: Custom types having string as their underlying types are now handled automatically by the driver. This answer predates the driver 1.x versions where this was necessary.


Unfortunately you're out of luck. The current state of the official mongo go driver does not support unmarshaling string values from BSON to a Go value whose type is a custom type having string as its underlying type. This may change in the future, but for now this is not supported.

The way decoding into a struct field is handled is implemented in bson/decode.go, currently line #387:

case 0x2:
    str := v.StringValue()
    switch containerType {
    case tString, tEmpty:
        val = reflect.ValueOf(str)
    case tJSONNumber:
        _, err := strconv.ParseFloat(str, 64)
        if err != nil {
            return val, err
        }
        val = reflect.ValueOf(str).Convert(tJSONNumber)

    case tURL:
        u, err := url.Parse(str)
        if err != nil {
            return val, err
        }
        val = reflect.ValueOf(u).Elem()
    default:
        return val, nil
    }

0x02 is the BSON string type. It is only attempted to decode into the struct field if the struct field's type is any of the following: string, interface{}, json.Number or url.URL (or a pointer to these).

Unfortunately implementing bson.Unmarshaler on your custom type does not help either, as it is not checked in case of struct fields, only if the struct itself implements it. But implementing on the struct itself, you would have to duplicate the struct with the field being one of the above listed supported types (or use a map or a bson.Document type).

This is a serious limitation on the library's part which can very easily be solved, so let's hope for the best that they add support for this in the near future.

Solution 2

I try to decode a DocumentResult into a struct using bson tags, and it does not work for a custom type wrapping a string

With your current MyType, the document that would be stored in MongoDB would be as below:

{
  "_id": ObjectId("..."),
  "some_int": NumberLong("42"),
  "some_string": "The Answer",
  "custom_type": "ABCD"
}

Even though the underlying type is a string, this could be tricky to decode with the current version of mongo-go-driver (v0.0.12) due to the type wrapping.

However, if you would like to have a custom type as such, you could change the struct into an embedded field instead. For example:

type MyDoc struct {
    SomeInt    int    `bson:"some_int"`
    SomeString string `bson:"some_string,omitempty"`
    CustomType MyType `bson:"custom_type,omitempty"`
}

type MyType struct {
    Value string `bson:"value,omitempty"`
}

var myType = MyType{Value: "ABCD"}

docToInsert := MyDoc{42, "The Answer", "ABCD"}

insertResult, err := collection.InsertOne(nil, docToInsert)

resultDoc := collection.FindOne(context.Background(), nil)
if err != nil {
    log.Fatal(err)
}
elem := &MyDoc{}
err = resultDoc.Decode(elem)
if err != nil {
    log.Fatal(err)
}
fmt.Println(elem.SomeInt, elem.SomeString, elem.CustomType.Value)
// 42 The Answer ABCD

The document would be stored in MongoDB as below:

{
  "_id": ObjectId("..."),
  "some_int": NumberLong("42"),
  "some_string": "The Answer",
  "custom_type": {
    "value": "ABCD"
  }
}

Otherwise just use string type directly because the resulting document in the database would be the same as the type wrapping version:

type MyDoc struct {
    SomeInt    int    `bson:"some_int"`
    SomeString string `bson:"some_string,omitempty"`
    CustomType string `bson:"custom_type,omitempty"`
} 

You may also find MongoDB Data Modeling a useful reference.

Solution 3

With the 1.x versions of MongoDB driver for Go (latest version at the time of writing is 1.3.1) it is fully possible to encode and decode aliased types.

Your example now works as expected, given one adjusts the mongo.Connect to match new 1.x API.

package main

import (
    "context"

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

type MyDoc struct {
    SomeInt    int    `bson:"some_int"`
    SomeString string `bson:"some_string,omitempty"`
    CustomType MyType `bson:"custom_type,omitempty"`
}

type MyType string

const myType MyType = "ABCD"

func main() {

    // Connect to db
    clientOpts := options.Client().
        ApplyURI("mongodb://localhost/example_db")
    client, _ := mongo.Connect(context.Background(), clientOpts)
    db := client.Database("example_db")
    collection := db.Collection("col")

    // Insert document
    docToInsert := MyDoc{42, "The Answer", myType}
    collection.InsertOne(nil, docToInsert)

    // Retrieve document
    filterDoc := MyDoc{SomeInt: 42}
    resultDoc := &MyDoc{}
    result := collection.FindOne(nil, filterDoc)
    result.Decode(resultDoc)

    println(resultDoc.SomeInt, resultDoc.SomeString, resultDoc.CustomType)
}

This returns: 42 The Answer ABCD as expected

Share:
16,002
amz
Author by

amz

Updated on June 17, 2022

Comments

  • amz
    amz almost 2 years

    I'm a beginner in both go and mongodb. I try to decode a DocumentResult into a struct using bson tags, and it does not work for a custom type wrapping a string. Can it be done without changing the field's type to a string?

        import (
        "context"
        "github.com/mongodb/mongo-go-driver/mongo"
    )
    
    type MyDoc struct {
        SomeInt int `bson:"some_int"`
        SomeString string `bson:"some_string,omitempty"`
        CustomType MyType `bson:"custom_type,omitempty"`
    }
    
    type MyType string
    
    const myType MyType = "ABCD"
    
    func main() {
    
        //Connect to db
        client, _ := mongo.Connect(context.Background(), "mongodb://localhost:27017", nil)
        db := client.Database("example_db")
        collection := db.Collection("col")
    
        //Insert document
        docToInsert := MyDoc{42, "The Answer", myType}
        collection.InsertOne(nil, docToInsert)
    
        //Retrieve document
        filterDoc := MyDoc{SomeInt: 42}
        resultDoc := &MyDoc{}
        result := collection.FindOne(nil, filterDoc)
        result.Decode(resultDoc)
    
        println(resultDoc.SomeInt, resultDoc.SomeString, resultDoc.CustomType)
    

    PRINTED RESULT: "42 The Answer" //"ABCD" is missing

    Thanks in advance