登录
首页 >  Golang >  Go问答

Go/Mongo 驱动程序:检索/索引未找到数据

来源:stackoverflow

时间:2024-03-13 22:36:27 279浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《Go/Mongo 驱动程序:检索/索引未找到数据》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

问题内容

我可以使用我的查询来过滤/搜索特定用户,我对此感到满意。但是,它没有返回我期望的正确或完整的数据量,如下所示。我需要包含生物和其他领域的数据。即使我尝试了下面的答案,我仍然返回错误的数据。这一定是我附加数据的方式?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
2
3
4
5
6
7
8
9
10
11
12
13
1:
avatar: ""
banner: ""
bio: ""
followercount: 0
followingcount: 0
likecount: 0
location: ""
name: ""
protected: false
treatcount: 0
username: "dev"
verified: false

getprofile 返回:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
}

尝试向您的 profile 模型添加您需要从数据库返回的每个参数的附加描述,以便它们完全匹配。因此,如果您的模型带有参数“name”,并且在 mongo 文档中它是“name”,那么您需要将其映射为“bson”。例如:

1
2
3
4
5
6
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

好了,本文到此结束,带大家了解了《Go/Mongo 驱动程序:检索/索引未找到数据》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>