ถูกแล้วที่จะดึงข้อมูลจาก mongodb ใน golang

0

คำถาม

ฉันมีเอกสารใน mongodb ในรูปแบบ,

{
  field1: string,
  field2: float64,
  field3: {...float64}
}

ในที่สุดฉันอยากจะเสมอไป field1&field2 และรับ/เลือกจาก field3 เดาใจ

ต้องทำมัน,ฉันถอดรหัสข้อมูลลงไปในเป็น struct แบบนี้

type MongoScore struct {
    field1          string              `json:"field1"`
    field2          float64             `json:"field2"`
    field3          map[string]float64  `json:"field3"`
}

คนส่วนหนึ่งฉันกำลังสงสัยคือไม่ว่ามันมีมากกว่ามีประสิทธิภาพเพียงพอ smooth scrolling วิธีที่จะดึงข้อมูลนั้นกับอย่างที่ต่างกัน

go mongodb
2021-11-23 22:29:06
1

คำตอบที่ดีที่สุด

1

ฉันคิดเสมอว่านายต้องติดตามข้อมูล struct:

type Product struct {
    ID          primitive.ObjectID `bson:"_id"`
    Title       string             `bson:"product"`
    Description string             `bson:"description"`

    count int64 // <- this is not exported, so data won't be filled
}

ใน Golang เพียงถูกส่งออกเป็นช่องข้อมูลหรือวิธีการของคือมารถเข้าใช้งานจากข้างนอกมันเป็นแพ็คเก็จฉันทำอยู่บ่อยๆ

ใช้ struct สนามป้ายกำกับต่างๆเพื่อบอก Mongodb โหลดข้อมูลจากคลังภาพตรงไปตรงกับฟิลด์ นี่ mongodb ตรงกับ bson ป้ายกำกับกับช่องข้อมูลในกคนจะได้ปลอดภัย

func main() {
    ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)

    client, err = mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017/"))
    if err != nil {
        log.Fatalf("can't connect to database: %v\n", err)
    }
    
    objID, _ := primitive.ObjectIDFromHex("619dd79acad38082f9ce16af")
    
    db := client.Database("db")
    col := db.Collection("products")
    
    filter := bson.D{
        {"_id", objID},
    }
    
    dest := &Project{}
    
    err := col.FindOne(ctx, filter).Decode(dest)
    if err != nil {
        log.Fatalln(err)
    }

จากวิธีการ Decode Unmarshals เจอข้อมูลเข้า dest.

2021-11-24 06:10:53

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................