Mongodb 从Mongo集合游标追加嵌套结构

Mongodb 从Mongo集合游标追加嵌套结构,mongodb,go,Mongodb,Go,我在向数组追加嵌套结构时遇到了一个问题 两者都有不同的嵌套值,但在追加最后一个结构时,它会覆盖第一个嵌套结构 下面是一个简单的概念示例。(我试图复制附加嵌套结构,但工作正常) 下面是我实际代码中的结构 类型主动结构{ OID primitive.ObjectID`bson:“\u id,ommitempty”json:“id,ommitempty”` Cid字符串`json:“Cid”` 名称字符串`json:“名称”` Description字符串`json:“Description”` Pr

我在向数组追加嵌套结构时遇到了一个问题

两者都有不同的嵌套值,但在追加最后一个结构时,它会覆盖第一个嵌套结构

下面是一个简单的概念示例。(我试图复制附加嵌套结构,但工作正常)

下面是我实际代码中的结构

类型主动结构{
OID primitive.ObjectID`bson:“\u id,ommitempty”json:“id,ommitempty”`
Cid字符串`json:“Cid”`
名称字符串`json:“名称”`
Description字符串`json:“Description”`
ProductName字符串`json:“产品名称”`
ProductId字符串`json:“产品\u id”`
Quarter字符串`json:“Quarter”`
年份字符串`json:“年”`
CustomFields[]NestedField`json:“自定义字段”`
}
类型NestedField结构{
Id字符串`json:“Id”`
Cid字符串`json:“Cid”`
名称字符串`json:“名称”`
类型字符串`json:“类型”`
表单字符串`json:“表单”`
值字符串`json:“值”`
}
类型InitiativeResponse结构{
ErrorCode字符串`json:“错误代码,省略为空”`
ErrorDescription字符串`json:“错误描述,省略为空”`
消息字符串`json:“消息,省略为空”`
有效载荷[]倡议`json:“有效载荷,省略空”`
}
这是我的职责

func GetInitiativesFromDB(d*database.MongoDB,cid字符串)(响应InitiativeResponse,错误){
筛选器:=bson.M{“cid”:cid}
cur,err:=d.Database.Collection(INITIATIVECOLLECTION).Find(context.Background(),filter,options.Find())
格式打印F(“%#v”,当前)
如果错误!=零{
返回响应,错误。错误响应{“未找到任何计划”,404}
}
风险值倡议
对于cur.Next(context.Background()){
错误:=当前解码(&I)
如果错误!=零{
返回响应,错误。错误响应{“未找到任何计划”,404}
}
response.Payload=append(response.Payload,initiative)
fmt.Printf(“%#v”,响应.有效载荷)
}
如果错误:=cur.err();错误!=nil{
返回响应,错误。错误响应{“未找到任何计划”,404}
}
_=cur.Close(context.Background())
返回响应,无
}
这是我在第一次和第二次追加后打印数组值时发现的

请注意,
NestedField
s第一次的值与第二次追加后的值不同

第一附加

[]api.Initiative{
    api.Initiative{
        OID:primitive.ObjectID{0x5d, 0xd9, 0x3f, 0x96, 0xd2, 0xc6, 0xb2, 0x95, 0x19, 0xd2, 0xbf, 0x98}, 
        Cid:"5d8502a2a284b46f3621f389", 
        Name:"1", 
        Description:"", 
        ProductName:"", 
        ProductId:"", 
        Quarter:"", 
        Year:"", 
        CustomFields:[]api.NestedField{
            api.NestedField{
                Id:"5db8ec9fee8040e9b6dfad87", 
                Cid:"5d8502a2a284b46f3621f389", 
                Name:"Test", 
                Type:"text", 
                Form:"initiative", 
                Value:"ggg"}, 
            api.NestedField{
                Id:"5dba0bcedf9cbf185683ecca", 
                Cid:"5d8502a2a284b46f3621f389", 
                Name:"Kylie", 
                Type:"text", 
                Form:"initiative", 
                Value:"ggg"}, 
            api.NestedField{
                Id:"5dd71d2af20bea1fef4564eb", 
                Cid:"5d8502a2a284b46f3621f389", 
                Name:"asdfasdf", 
                Type:"text", 
                Form:"initiative", 
                Value:"ggg"}}}}
第二附加


[]api.Initiative{
    api.Initiative{
        OID:primitive.ObjectID{0x5d, 0xd9, 0x3f, 0x96, 0xd2, 0xc6, 0xb2, 0x95, 0x19, 0xd2, 0xbf, 0x98}, 
        Cid:"5d8502a2a284b46f3621f389", 
        Name:"1", 
        Description:"", 
        ProductName:"", 
        ProductId:"", 
        Quarter:"", 
        Year:"", 
        CustomFields:[]api.NestedField{
            api.NestedField{
                Id:"5db8ec9fee8040e9b6dfad87", 
                Cid:"5d8502a2a284b46f3621f389", 
                Name:"Test", 
                Type:"text", 
                Form:"initiative", 
                Value:"aaa"}, 
            api.NestedField{
                Id:"5dba0bcedf9cbf185683ecca", 
                Cid:"5d8502a2a284b46f3621f389", 
                Name:"Kylie", 
                Type:"text", 
                Form:"initiative", 
                Value:"aaa"}, 
            api.NestedField{
                Id:"5dd71d2af20bea1fef4564eb", 
                Cid:"5d8502a2a284b46f3621f389", 
                Name:"asdfasdf", 
                Type:"text", 
                Form:"initiative", 
                Value:""}}}, 
    api.Initiative{
        OID:primitive.ObjectID{0x5d, 0xd9, 0x3f, 0x9f, 0xd2, 0xc6, 0xb2, 0x95, 0x19, 0xd2, 0xbf, 0x99}, 
        Cid:"5d8502a2a284b46f3621f389", 
        Name:"2", 
        Description:"", 
        ProductName:"", 
        ProductId:"", 
        Quarter:"", 
        Year:"", 
        CustomFields:[]api.NestedField{
            api.NestedField{
                Id:"5db8ec9fee8040e9b6dfad87", 
                Cid:"5d8502a2a284b46f3621f389", 
                Name:"Test", 
                Type:"text", 
                Form:"initiative", 
                Value:"aaa"}, 
            api.NestedField{
                Id:"5dba0bcedf9cbf185683ecca", 
                Cid:"5d8502a2a284b46f3621f389", 
                Name:"Kylie", 
                Type:"text", 
                Form:"initiative", 
                Value:"aaa"}, 
            api.NestedField{
                Id:"5dd71d2af20bea1fef4564eb", 
                Cid:"5d8502a2a284b46f3621f389", 
                Name:"asdfasdf", 
                Type:"text",
                Form:"initiative", 
                Value:""}}}}
以及打印cur的结果

&mongo.Cursor{
    Current:bson.Raw(nil), 
    bc:(*driver.BatchCursor)(0xc000204000), 
    batch:(*bsoncore.DocumentSequence)(nil), 
    registry:(*bsoncodec.Registry)(0xc0000ee070), 
    clientSession:(*session.Client)(0xc0001f40c0), err:error(nil)}

有人知道为什么会发生这种情况,或者如何修复它吗?

我发现,当我在循环中移动
var initiative
时,它工作得很好

func GetInitiativesFromDB(d *database.MongoDB, cid string) (response InitiativeResponse, err error) {
    filter := bson.M{"cid": cid}
    cur, err := d.Database.Collection(INITIATIVECOLLECTION).Find(context.Background(), filter, options.Find())
    fmt.Printf("%#v", cur)
    if err != nil {
        return response, errors.ErrorResponse{"No initiatives were found", 404}
    }
    for cur.Next(context.Background()) {
        var initiative Initiative
        err := cur.Decode(&initiative)
        if err != nil {
            return response, errors.ErrorResponse{"No initiatives were found", 404}
        }
        response.Payload = append(response.Payload, initiative)
        fmt.Printf("%#v", response.Payload)
    }
    if err := cur.Err(); err != nil {
        return response, errors.ErrorResponse{"No initiatives were found", 404}
    }
    _ = cur.Close(context.Background())
    return response, nil
}

我发现当我在循环中移动
var-initiative
时,它工作得很好

func GetInitiativesFromDB(d *database.MongoDB, cid string) (response InitiativeResponse, err error) {
    filter := bson.M{"cid": cid}
    cur, err := d.Database.Collection(INITIATIVECOLLECTION).Find(context.Background(), filter, options.Find())
    fmt.Printf("%#v", cur)
    if err != nil {
        return response, errors.ErrorResponse{"No initiatives were found", 404}
    }
    for cur.Next(context.Background()) {
        var initiative Initiative
        err := cur.Decode(&initiative)
        if err != nil {
            return response, errors.ErrorResponse{"No initiatives were found", 404}
        }
        response.Payload = append(response.Payload, initiative)
        fmt.Printf("%#v", response.Payload)
    }
    if err := cur.Err(); err != nil {
        return response, errors.ErrorResponse{"No initiatives were found", 404}
    }
    _ = cur.Close(context.Background())
    return response, nil
}

你能:1。将
fmt.Println(response.Payload)
更改为
fmt.Printf(“%#v”,response.Payload)
?2.将输出作为实际代码添加到问题中,而不是图像(如果图像变得不可用,您的问题将变得毫无意义)?3.将
db.yourcollection.find({“cid”:cid})
的输出也添加到您的问题中?@MarkusWMahlberg我刚刚做了这些更改。希望它能有所帮助。我指的是mongo shell的输出,目的是检查字段,并将它们与预期的输出进行比较;)你能:1。将
fmt.Println(response.Payload)
更改为
fmt.Printf(“%#v”,response.Payload)
?2.将输出作为实际代码添加到问题中,而不是图像(如果图像变得不可用,您的问题将变得毫无意义)?3.将
db.yourcollection.find({“cid”:cid})
的输出也添加到您的问题中?@MarkusWMahlberg我刚刚做了这些更改。希望它能有所帮助。我指的是mongo shell的输出,目的是检查字段,并将它们与预期的输出进行比较;)