我可以使用我的查詢來過濾/搜索一個特定的用戶,我對此很滿意。然而,它沒有回傳我所期望的正確或完整的資料量,如下圖所示。我需要包括生物和其他領域的資料。即使我嘗試了下面的答案,我仍然回傳錯誤的資料。這一定是我追加資料的方式問題。
type Profile struct {
用戶名 string
已驗證 bool
受保護 bool
頭像 string
旗幟 string
關注者數量 int32
關注人數 int32
對待計數 int32
LikeCount int32
名字 string
生物 string
網站string
地點string
}
func SearchProfiles(filter string) ([]model. Profile, error) {
results :=[]models.Profile{}。
filterr := bson.D{{鍵。"用戶名", Value: primitive.Regex{Pattern: filter, Options: ""}}}。
cur, err := userColl.Find(ctx, filterr)
if err != nil {
log.Fatal()。
Err(err)。
Msg("err1")
}
for cur.Next(context.TODO() ) {
var elem models.Profile
err := cur.Decode(&elem)
fmt.Println(elem)
if err != nil {
log.Fatal()。
Err(err)。
Msg("err2")
}
結果 = append(results, elem)
}
if err := cur.Err(); err != nil {
log.Fatal()。
Err(err)。
Msg("err3")
}
cur.Close(context.TODO())
return results, nil
}
searchProfiles回傳這個:
1:
avatar: "".
banner: "": "
Bio: ""
followerCount: 0
followingCount: 0
likeCount: 0
location: ""/span>
名稱: ""
protected: false
treatCount: 0
用戶名: "dev"
驗證: false
getProfile的回報是:
...
profile:Object。
用戶名:"dev"。
驗證:false
protected:false。
avatar:""
banner:""/span>
followercount:163。
followingcount:15。
treatcount:13
likecount:612
name:" developer"
bio:"23, ayooo"。
website:""。
location:"afk"。
uj5u.com熱心網友回復:
嘗試在你的Profile模型中添加你需要從db回傳的每個引數的額外描述,以便它們完全匹配。因此,如果你的模型有引數'Name',而在你的mongo檔案中它是'name',那么你需要將它映射為'bson'。例如:
type Profile struct {
***
Name string `json:"name" bson:"name"`.
TreatCount int32 `json:"treatcount" bson:"treatcount"`。
***
}
uj5u.com熱心網友回復:
這就是我追加資料的方式!
func SearchProfiles(filter string) ([]model. Profile, error) {
profiles :=[]models.Profile{}
user := models.User{}
filterr := bson.D{{鍵。"用戶名", Value: primitive.Regex{Pattern: filter, Options: ""}}}。
cur, err := userColl.Find(ctx, filterr)
if err != nil {
log.Fatal()。
Err(err)。
Msg("err1")
}
for cur.Next(context.TODO() ) {
err := cur.Decode(&user)
if err != nil {
log.Fatal()。
Err(err)。
Msg("err2")
}
profiles = append(profile, user.Profile)
}
if err := cur.Err(); err != nil {
log.Fatal()。
Err(err)。
Msg("err3")
}
cur.Close(context.TODO())
return profiles, nil
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/313763.html
標籤:
