【发布时间】:2021-02-02 05:40:44
【问题描述】:
我正在使用grpc+golang+mongodb,并且我有以下proto 文件。
enum InventoryType {
LARGE = 0;
SMALL = 1;
}
我的代码:
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"inventory-service/pb"
)
func (i *Inventory) CreateInventory(ctx context.Context, req *pb.CreateInventoryRequest) (*pb.CreateInventoryResponse, error) {
inventory := req.GetInventory()
data := pb.Inventory{
Inventory: inventory.GetInventory(),
}
mctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, _ := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
collection := client.Database("test_db").Collection("test_collection")
collection.InsertOne(mctx, data)
return &pb.CreateInventoryResponse{}, nil
}
当我使用 golang 将枚举保存到 mongodb 时,它会保存 int 值 0、1 而不是 'LARGE'、'SMALL',关于如何保存字符串的任何想法?
【问题讨论】:
标签: mongodb go protocol-buffers grpc