Go/Mongo驱动程序:索引/查找未返回数据

huangapple go评论92阅读模式
英文:

Go/Mongo Driver: Index/Find isn't returning data

问题

我可以使用我的查询来过滤/搜索特定用户,并且对此感到满意。然而,它没有返回我期望的正确或完整的数据量,如下所示。我需要包括个人简介和其他字段的数据。即使我尝试下面的答案,我仍然返回错误的数据。可能是我如何附加数据的问题?

type Profile struct {
	Username        string
	Verified        bool
	Protected       bool
	Avatar          string
	Banner          string
	FollowerCount   int32
	FollowingCount  int32
	TreatCount      int32
	LikeCount       int32
	Name            string
	Bio             string
	Website         string
	Location        string
}
func SearchProfiles(filter string) ([]models.Profile, error) {
	results := []models.Profile{}

	filterr := bson.D{{Key: "username", 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")
		}
		results = 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: ""
name: ""
protected: false
treatCount: 0
username: "dev"
verified: false

getProfile 返回以下内容:

...
profile:Object
username:"dev"
verified:false
protected:false
avatar:""
banner:""
followercount:163
followingcount:15
treatcount:13
likecount:612
name:"developer"
bio:"23, ayooo"
website:""
location:"afk"
英文:

I can use my query to filter/search for a specific user and I'm happy with that. However, It doesn't return the correct or full amount of data that I'm expecting, as shown below. I need the bio and other fields of data to be included. Even as I tried the answer down below I'm still returning the wrong data. It must be how I'm appending the data?

type Profile struct {
	Username string

	Verified  bool
	Protected bool

	Avatar string
	Banner string

	FollowerCount  int32
	FollowingCount int32
	TreatCount     int32
	LikeCount      int32

	Name     string
	Bio      string
	Website  string
	Location string
}
func SearchProfiles(filter string) ([]models.Profile, error) {
	results := []models.Profile{}

	filterr := bson.D{{Key: "username", 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")
		}
		results = append(results, elem)
	}

	if err := cur.Err(); err != nil {
		log.Fatal().
			Err(err).
			Msg("err3")
	}

	cur.Close(context.TODO())

	return results, nil
}

searchProfiles returns this:

1:
avatar: ""
banner: ""
bio: ""
followerCount: 0
followingCount: 0
likeCount: 0
location: ""
name: ""
protected: false
treatCount: 0
username: "dev"
verified: false

getProfile returns this:

...
profile:Object
username:"dev"
verified:false
protected:false
avatar:""
banner:""
followercount:163
followingcount:15
treatcount:13
likecount:612
name:"developer"
bio:"23, ayooo"
website:""
location:"afk"

答案1

得分: 1

尝试在您的Profile模型中为每个需要从数据库返回的参数添加额外的描述,以确保它们完全匹配。因此,如果您的模型具有参数'Name',而在您的Mongo文档中它是'name',那么您需要使用'bson'进行映射。例如:

type Profile struct {
  ***
  Name string `json:"name" bson:"name"`
  TreatCount int32  `json:"treatcount" bson:"treatcount"`
  ***
}

在这里阅读更多信息:https://www.mongodb.com/blog/post/quick-start-golang--mongodb--modeling-documents-with-go-data-structures

英文:

Try to add to your Profile model additional description of every parameter that you need returned from db so they match exactly. So if you have your model with parameter 'Name' and in your mongo document it's 'name' than you need to map it with 'bson'. For example:

type Profile struct {
  ***
  Name string `json:"name" bson:"name"`
  TreatCount int32  `json:"treatcount" bson:"treatcount"`
  ***
}

Read more here: https://www.mongodb.com/blog/post/quick-start-golang--mongodb--modeling-documents-with-go-data-structures

答案2

得分: 0

这是我翻译好的内容:

这是我如何添加数据的方式!

func SearchProfiles(filter string) ([]models.Profile, error) {
	profiles := []models.Profile{}
	user := models.User{}

	filterr := bson.D{{Key: "username", 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(profiles, user.Profile)
	}

	if err := cur.Err(); err != nil {
		log.Fatal().
			Err(err).
			Msg("err3")
	}

	cur.Close(context.TODO())

	return profiles, nil
}
英文:

It was how I was appending the data!!

func SearchProfiles(filter string) ([]models.Profile, error) {
	profiles := []models.Profile{}
	user := models.User{}

	filterr := bson.D{{Key: "username", 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(profiles, user.Profile)
	}

	if err := cur.Err(); err != nil {
		log.Fatal().
			Err(err).
			Msg("err3")
	}

	cur.Close(context.TODO())

	return profiles, nil
}

huangapple
  • 本文由 发表于 2021年9月14日 17:50:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/69175590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定