如何使用Golang处理来自MongoDB文档的数据?

huangapple go评论119阅读模式
英文:

How to handle data from MongoDB document with Golang?

问题

我使用FindOne()获取到了一个文档。

这是在golang中表示文档的方式:

  1. map[
  2. _id:ObjectID("12")
  3. chatID:12
  4. expenses:[
  5. map[amount:12 category:food]
  6. map[amount:14 category:food]
  7. ]
  8. income:[]
  9. ]

这是在MongoDB Atlas中的表示方式:

  1. {
  2. "_id":{"$oid":"12"},
  3. "chatID":{"$numberInt":"12"},
  4. "expenses":[
  5. {"category":"food","amount":{"$numberDouble":"12.0"}},
  6. {"category":"food","amount":{"$numberDouble":"14.0"}}
  7. ],
  8. "income":[]
  9. }

如何逐行处理这些数据?例如,如何打印每个支出的类别和金额?

英文:

I get document with FindOne()

This is how document is presented in the golang:

  1. map[
  2. _id:ObjectID("12")
  3. chatID:12
  4. expenses:[
  5. map[amount:12 category:food]
  6. map[ amount:14 category:food]]
  7. income:[]]

This is how in MongoDB Atlas:

  1. {"_id":{"$oid":"12"},
  2. "chatID":{"$numberInt":"12"},
  3. "expenses":[
  4. ​{"category":"food","amount":{"$numberDouble":"12.0"}},
  5. ​{"category":"food","amount":{"$numberDouble":"14.0"}}],
  6. "income":[]}

How to work with each line separately? For example, how to print category and amount of every expense?

答案1

得分: 2

以下是翻译好的内容:

  1. type List struct {
  2. Category string `bson:"category"`
  3. Amount float32 `bson:"amount"`
  4. }
  5. type Document struct {
  6. ID primitive.ObjectID `bson:"_id,omitempty"`
  7. ChatID int `bson:"chatID"`
  8. Expenses []List `bson:"expense"`
  9. Income []List `bson:"income"`
  10. }
  11. myDoc := Document
  12. client.Collection.FindOne(context.TODO(), bson.D{}).Decode(&myDoc)
英文:
  1. type List struct {
  2. Category string `bson:"category"`
  3. Amount float32 `bson:""amount"`
  4. }
  5. type Document struct {
  6. ID primitive.ObjectID `bson:"_id, omitempty"`
  7. ChatID int `bson:"chatID"`
  8. Expenses []List `bson:"expense"`
  9. Income []List `bson:"income"`
  10. }
  11. myDoc := Document
  12. client.Collection.FindOne(context.TODO(), bson.D{}).Decode(&myDoc)

答案2

得分: 0

你需要一个用于这个MongoDB文档的结构体:

  1. type Fulfillment struct {
  2. Id primitive.ObjectID `bson:"_id"`
  3. ChatID int64 `bson:"chatID"`
  4. Expenses []map[string]interface{} `bson:"expenses"`
  5. }

如果你使用MongoDB官方库:

  1. var fulfillment Fulfillment
  2. err = client.Database("test_data").Collection("fulfillment").FindOne(ctx, bson.M{"chatID": 100000000012}).Decode(&fulfillment)
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. fmt.Printf("%d\n", fulfillment.ChatID)
  7. fmt.Printf("%s\n", fulfillment.Id.Hex())
  8. for _, expensive := range fulfillment.Expenses {
  9. for k, v := range expensive {
  10. switch v.(type) {
  11. case string:
  12. fmt.Printf("%s: %s \n", k, v)
  13. case float64:
  14. fmt.Printf("%s: %f \n", k, v)
  15. }
  16. }
  17. }

希望能帮到你。

英文:

You need a struct for this MongoDB document:

  1. type Fulfillment struct {
  2. Id primitive.ObjectID `bson:"_id"`
  3. ChatID int64 `bson:"chatID"`
  4. Expenses []map[string]interface{} `bson:"expenses"`
  5. }

if you use MongoDB official library:

  1. var fulfillment Fulfillment
  2. err = client.Database("test_data").Collection("fulfillment").FindOne(ctx, bson.M{"chatID": 100000000012}).Decode(&fulfillment)
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. fmt.Printf("%d\n", fulfillment.ChatID)
  7. fmt.Printf("%s\n", fulfillment.Id.Hex())
  8. for _, expensive := range fulfillment.Expenses {
  9. for k, v := range expensive {
  10. switch v.(type) {
  11. case string:
  12. fmt.Printf("%s: %s \n", k, v)
  13. case float64:
  14. fmt.Printf("%s: %f \n", k, v)
  15. }
  16. }
  17. }

I hope I've helped

huangapple
  • 本文由 发表于 2021年11月23日 18:38:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/70079370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定