亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我從 mongodb 獲取 json 中某些字段的值全為零?

為什么我從 mongodb 獲取 json 中某些字段的值全為零?

Go
泛舟湖上清波郎朗 2023-07-26 16:31:46
我正在使用官方 mongodb-go-driver 從 Go Web 服務器中的 MongoDB 圖集獲取數據。我正在使用 json.Marshal 轉換為 json。但某些字段的所有值都變為零。package mainimport ("context""fmt""log""github.com/gin-gonic/gin""go.mongodb.org/mongo-driver/bson""go.mongodb.org/mongo-driver/mongo""go.mongodb.org/mongo-driver/mongo/options""go.mongodb.org/mongo-driver/mongo/readpref")var c = GetClient()type PlantData struct {Minute       int     `json:"minute"`Date         int     `json:"date"`Moisture1    int     `json:"moisture_1"`Hour         int     `json:"hour"`Month        int     `json:"month"`Year         int     `json:"year"`Humidity1    float64 `json:"humidity_1"`Temperature1 float64 `json:"temperature_1"`}func GetClient() *mongo.Client {    clientOptions := options.Client().ApplyURI("MY_MONGODB_URI")    client, err := mongo.NewClient(clientOptions)    if err != nil {        log.Fatal(err)    }    err = client.Connect(context.Background())    if err != nil {        log.Fatal(err)    }    return client}func ReturnAllPlantsData(client *mongo.Client, filter bson.M) []*PlantData {    var plantsdata []*PlantData    collection := client.Database("iot").Collection("tomatos")    cur, err := collection.Find(context.TODO(), filter)    if err != nil {        log.Fatal("Error on Finding all the documents", err)    }    for cur.Next(context.TODO()) {        var plantdata PlantData        err = cur.Decode(&plantdata)        if err != nil {            log.Fatal("Error on Decoding the document", err)        }        plantsdata = append(plantsdata, &plantdata)    }    return plantsdata}func getting(g *gin.Context) {       plantsdatas := ReturnAllPlantsData(c, bson.M{})     ans, _ := json.Marshal(plantsdatas)     fmt.Println(string(ans))     c.String(200, string(ans))}
查看完整描述

1 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

在從 MongoDB 編組或編組到 MongoDB 時必須使用bson標簽。json標簽用于encoding/json包,Mongo 驅動程序不使用(忽略)它們。


type PlantData struct {

    Minute       int     `bson:"minute"`

    Date         int     `bson:"date"`

    Moisture1    int     `bson:"moisture_1"`

    Hour         int     `bson:"hour"`

    Month        int     `bson:"month"`

    Year         int     `bson:"year"`

    Humidity1    float64 `bson:"humidity_1"`

    Temperature1 float64 `bson:"temperature_1"`

}

如果bson您的結構字段中缺少標簽,MongoDB 中使用的默認名稱將以小寫字母開頭的結構字段名稱,這就是為什么某些(大多數)字段匹配但不匹配的原因(它的不同之處不僅僅是大寫首Moisture1字母moisture_1)。


如果您還想使用encoding/json帶有此結構的包,您可以提供兩者:


type PlantData struct {

    Minute       int     `bson:"minute" json:"minute"`

    Date         int     `bson:"date" json:"date"`

    Moisture1    int     `bson:"moisture_1" json:"moisture_1"`

    Hour         int     `bson:"hour" json:"hour"`

    Month        int     `bson:"month" json:"month"`

    Year         int     `bson:"year" json:"year"`

    Humidity1    float64 `bson:"humidity_1" json:"humidity_1"`

    Temperature1 float64 `bson:"temperature_1" json:"temperature_1"`

}


查看完整回答
反對 回復 2023-07-26
  • 1 回答
  • 0 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號