른 방법으로 데이터를 끌어에서 mongodb 에프로그램

0

질문

나는 문서에 mongodb 에서 이 형식

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

궁극적으로 나는 항상 필드 1&필드 2 고/에서 선택 필드 3 은 개체입니다.

그것을 할 나는 디코딩으로 데이터 구조체에 다음과 같이,

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

부품 나이 있는지 여부를 더 효율적으로 접근 방식을 이 데이터를 다른 유형이 있습니다.

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

최고의 응답

1

가정하면 다음과 같은 데이터 구조체:

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
}

에작만 수출 분야나 방법에서 액세스할 수 있어 패키지입니다.

용 구조체 필드 태그를 말 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ý
..................................................................................................................