第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 Go api 中過(guò)濾從 MongoDB 顯示的信息

在 Go api 中過(guò)濾從 MongoDB 顯示的信息

Go
千萬(wàn)里不及你 2022-11-08 16:36:45
我是 Go 和 React 的新手,我在這個(gè)迷你項(xiàng)目中使用了這兩者。Go 正在使用 Mongodb 運(yùn)行后端 api。我在 Go 中從 Mongo 獲取用戶列表,然后將其發(fā)送到 React,問(wèn)題是 Mongo 給了我用戶的所有字段(_id、密碼和用戶名),我只想要用戶名。我不明白如何過(guò)濾它并防止所有字段從 Go 發(fā)送到 React。Go API 的 JSON 輸出:{    "Success": true,    "Data": [        {            "_id": "6205ac3d72c15c920a424608",            "password": {                "Subtype": 0,                "Data": "removed for security"            },            "username": "removed for security"        },        {            "_id": "6206b44afb8b044fdba76b8f",            "password": {                "Subtype": 0,                "Data": "removed for security"            },            "username": "removed for security"        }    ]}這是我指定路線的 Go 代碼:// Route: Get Users, for getting a list of usersfunc RouteGetUsers(w http.ResponseWriter, r *http.Request, p httprouter.Params) {    // Set content-type to JSON    w.Header().Set("Content-Type", "application/json")    type Response struct {        Success bool     `json:"Success"`        Data    []bson.M `json:"Data"`    }    // Load the env file    err := godotenv.Load("variables.env")    if err != nil {        log.Fatal("Error loading .env file")    }    // Get Mongo DB environment variable    uri := os.Getenv("MONGO_URI")    if uri == "" {        log.Fatal("You must set your 'MONGO_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/#environment-variable")    }    // Connect to Mongo Database    client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))    if err != nil {        panic(err)    }    // Close the database connection at the end of the function    defer func() {        if err := client.Disconnect(context.TODO()); err != nil {            panic(err)        }    }()}
查看完整描述

1 回答

?
蠱毒傳說(shuō)

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊

使用投影選項(xiàng):


opts := options.Find().SetProjection(bson.D{{"username", 1}})

cursor, err := coll.Find(context.TODO(), bson.D{}, opts)

作為第二種方法:使用您想要的字段聲明一個(gè)類型,并獲取該類型。


type Data struct {

   Username string `bson:"username" json:"username"`

}


...


var data []Data

if err = cursor.All(context.TODO(), &data); err != nil { ...


...


var response = struct {

    Success bool     `json:"Success"`

    Data    []Data   `json:"Data"`

}{

    true,

    data,

}

responseJson, err := json.Marshal(response)

...

第三種方法:過(guò)濾問(wèn)題中的地圖:


for _, result := range results {

    for k := range result {

       if k != "username" {

          delete(result, k)

       }

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-11-08
  • 1 回答
  • 0 關(guān)注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)