jlucktay
jlucktay

Reputation: 432

UUID as _id error - can't use an array for _id

I'm attempting to use UUIDs for my _id field in a MongoDB.

I have a wrapper struct to hold my record, like so:

type mongoWrapper struct {
    ID      uuid.UUID       `bson:"_id" json:"_id"`
    Payment storage.Payment `bson:"payment" json:"payment"`
}

This is my code around the InsertOne function from the MongoDB driver packages:

func (s *Storage) Create(newPayment storage.Payment) (uuid.UUID, error) {
    mongoInsert := wrap(newPayment)
    c := s.client.Database(thisDatabase).Collection(thisCollection)

    insertResult, errInsert := c.InsertOne(context.TODO(), mongoInsert)
    if errInsert != nil {
        return uuid.Nil, errInsert
    }

    fmt.Println("Inserted a single document: ", insertResult.InsertedID)

    return mongoInsert.ID, nil
}

This is my wrap() func that wraps up the payment record data, and takes an optional UUID argument or generates its own accordingly:

func wrap(p storage.Payment, i ...uuid.UUID) *mongoWrapper {
    mw := &mongoWrapper{ID: uuid.Nil, Payment: p}

    if len(i) > 0 {
        mw.ID = i[0]
        return mw
    }

    newID, errID := uuid.NewV4()
    if errID != nil {
        log.Fatal(errID)
    }

    mw.ID = newID

    return mw
}

When one of my tests calls Create() it returns the following error:

storage_test.go:38: err: multiple write errors: [{write errors: [{can't use an array for _id}]}, {<nil>}]

I'm using the following packages for my UUIDs and MongoDB driver:

import(
    uuid "github.com/satori/go.uuid"

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

I'm not clear on where exactly the problem lies.

Do I need some extra plumbing around the UUID in order for it to be handled correctly?

edit: I made some more changes, but the UUID still comes through as an array:

type mongoWrapper struct {
    UUID    mongoUUID       `bson:"uuid" json:"uuid"`
    Payment storage.Payment `bson:"payment" json:"payment"`
}

type mongoUUID struct {
    uuid.UUID
}

func (mu *mongoUUID) MarshalBSON() ([]byte, error) {
    return []byte(mu.UUID.String()), nil
}

func (mu *mongoUUID) UnmarshalBSON(b []byte) error {
    mu.UUID = uuid.FromStringOrNil(string(b))
    return nil
}

Upvotes: 1

Views: 2158

Answers (2)

jlucktay
jlucktay

Reputation: 432

I came to the below implementation as a solution for this:

type mongoUUID struct {
    uuid.UUID
}

func (mu mongoUUID) MarshalBSONValue() (bsontype.Type, []byte, error) {
    return bsontype.Binary, bsoncore.AppendBinary(nil, 4, mu.UUID[:]), nil
}

func (mu *mongoUUID) UnmarshalBSONValue(t bsontype.Type, raw []byte) error {
    if t != bsontype.Binary {
        return fmt.Errorf("invalid format on unmarshal bson value")
    }

    _, data, _, ok := bsoncore.ReadBinary(raw)
    if !ok {
        return fmt.Errorf("not enough bytes to unmarshal bson value")
    }

    copy(mu.UUID[:], data)

    return nil
}

Upvotes: 1

Jonathan Hall
Jonathan Hall

Reputation: 79586

uuid.UUID is a [16]byte under the hood.

However, this type also implements the encoding.TextMarshaler interface, which I would expect mongo to honor (the same way the json package does).

I believe the solution is to create your own type, which embeds the uuid.UUID type, and provides a custom implementation of the bson.Marshaler interface.

Upvotes: 2

Related Questions