When storing a github.com/google/uuid
UUID field in MongoDB using Golang it translates to a base64 binary with subtype 0. This makes it impossible to naturally query the document field by UUID.
The inserted user looks like:
{"_id":{"$binary":"0bHYoNWSTV+KqWSl54YWiQ==","$type":"0"},"name":"Isabella"}
When querying by the generated UUID d1b1d8a0-d592-4d5f-8aa9-64a5e7861689
, the results are empty.
type User struct {
UserId uuid.UUID `json:"userId" bson:"_id"`
Name string `json:"name" bson:"name"`
}
func (repo userRepo) User(uuidIn uuid.UUID) (model.User, error) {
collection := repo.database.Collection(mongoCollectionUser)
var user model.User
err := collection.FindOne(context.Background(),
bson.M{"_id": uuidIn},
).Decode(&user)
// err: mongo: no documents in result
}