问题:如何使用 Golang 在 MongoDB 中存储 UUID?
github.com/google/uuid
插入的用户如下所示:
{"_id":{"$binary":"0bHYoNWSTV+KqWSl54YWiQ==","$type":"0"},"name":"Isabella"}
d1b1d8a0-d592-4d5f-8aa9-64a5e7861689
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
}
解答
github.com/google/uuid zwz100007 16]byte