Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

375
Views
Cómo almacenar enumeración como cadena en lugar de int en Mongodb

Estoy usando grpc + golang + mongodb y tengo el siguiente archivo proto .

 enum InventoryType { LARGE = 0; SMALL = 1; }

mi código de ir:

 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 }

y cuando guardo la enumeración en mongodb usando golang, guarda el valor int 0, 1 en lugar de 'GRANDE', 'PEQUEÑO', ¿alguna idea sobre cómo puedo guardar una cadena en su lugar?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Protobuf es para comunicación , no para modelado de base de datos. No debe usar archivos generados por protobuf para guardar en su base de datos.

En su lugar, cree un tipo separado que modele el documento que desea almacenar en la base de datos, en el que puede almacenar la representación de cadena de su enumeración, y eso se almacenará en la base de datos.

Por ejemplo:

 type MyData { Inventory string `bson:"inventory"` }

Y usándolo:

 data := MyData{ Inventory: inventory.GetInventory().String(), }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!