如何在嵌套数组中推送golang?

时间:2016-05-24 07:53:27

标签: mongodb go mgo

我尝试使用$push在嵌套数组中推送一些数据。这是我的json文件

{
    "_id" : ObjectId("57307906f051147d5317984e"),
    "user" : [
        {
            "firstName" : "chetan",
            "lastName" : "kumar",
            "age" : 23,
            "sales" : [
                {
            "firstName" : "ashu",
            "lastName" : "jha",
            "age" : 27
                }
            ],
        },
        {
            "firstName" : "nepolean",
            "lastName" : "dang",
            "age" : 26
        },
        {
            "firstName" : "Raj",
            "lastname" : "kumar",
            "age" : 26
        }
    ],

}

现在这是我在这里尝试执行的golang代码

package main

import(
    "fmt"
    "log"
    "net/http"
        //"encoding/json"
    "github.com/gorilla/mux"
        "gopkg.in/mgo.v2"
        "gopkg.in/mgo.v2/bson"
)
type User struct{
    SALES       []Sales     `json:"sales" bson:"sales"`
    FIRSTNAME   string      `json:"firstName" bson:"firstName"`
    LASTNAME    string      `json:"lastName" bson:"lastName"`
    AGE     int     `json:"age" bson:"age"`
}
type Sales struct{
    FIRSTNAME   string      `json:"firstName" bson:"firstName"`
    LASTNAME    string      `json:"lastName" bson:"lastName"`
    AGE     int     `json:"age" bson:"age"`
}

type Details struct{
    ID  bson.ObjectId   `json:"_id" bson:"_id"`
    USER    []User      `json:"user" bson:"user"`

}


func detail(w http.ResponseWriter, r *http.Request){
    session, err := mgo.Dial("127.0.0.1")
        if err != nil {
                panic(err)
        }else{
                fmt.Println("dial")
        }
        defer session.Close()


        session.SetMode(mgo.Monotonic, true)

        c := session.DB("userdb").C("user")
        addsales:= []Sales{Sales{
            FIRSTNAME : "chetan",
            LASTNAME : "kumar",
            AGE : 24,
            },
        }
    fmt.Println(addsales)
    match := bson.M{"firstName" : "nepolean"}
    change := bson.M{"$push":bson.M{"user.$.sales":addsales}}
    err = c.Update(match,change)
    if err !=nil{
        panic(err)      
    }else{
    fmt.Println("success")
    }
}

func main(){
    router := mux.NewRouter().StrictSlash(true)
    router.HandleFunc("/detail",detail)
    log.Fatal(http.ListenAndServe(":9080", router))

}

这是我执行

后的OutPut
/Desktop$ go run arrayupdate.go dial [{chetan kumar 24}] 2016/05/24
 12:39:48 http: panic serving 127.0.0.1:43928: not found goroutine 5
 [running]: net/http.(*conn).serve.func1(0xc820082200)
    /usr/local/go/src/net/http/server.go:1389 +0xc1 panic(0x7c6bc0,
 0xc82000b080)  /usr/local/go/src/runtime/panic.go:426 +0x4e9
 main.detail(0x7f4a62bf9900, 0xc8200735f0, 0xc8200d6000)
    /home/joybynature/Desktop/arrayupdate.go:93 +0x7a6
 net/http.HandlerFunc.ServeHTTP(0x91d9f0, 0x7f4a62bf9900, 0xc8200735f0,
 0xc8200d6000)  /usr/local/go/src/net/http/server.go:1618 +0x3a
 github.com/gorilla/mux.(*Router).ServeHTTP(0xc820012550,
 0x7f4a62bf9900, 0xc8200735f0, 0xc8200d6000)
    /home/joybynature/src/github.com/gorilla/mux/mux.go:107 +0x285
 net/http.serverHandler.ServeHTTP(0xc820082180, 0x7f4a62bf9900,
 0xc8200735f0, 0xc8200d6000)    /usr/local/go/src/net/http/server.go:2081
 +0x19e net/http.(*conn).serve(0xc820082200)    /usr/local/go/src/net/http/server.go:1472 +0xf2e created by
 net/http.(*Server).Serve   /usr/local/go/src/net/http/server.go:2137
 +0x44e

2 个答案:

答案 0 :(得分:5)

您正在推送一个Sales数组,其中包含单个项目的语法 它应该是要推送的单个Sales对象:

addsales:= Sales{
    FIRSTNAME : "chetan",
    LASTNAME : "kumar",
    AGE : 24,
},
change := bson.M{"$push":bson.M{"user.$.sales":addsales}}

或使用$each修饰符推送多个项目:

addsales:= []Sales{Sales{
    FIRSTNAME : "chetan",
    LASTNAME : "kumar",
    AGE : 24,
    },
}
change := bson.M{"$push":bson.M{"user.$.sales":bson.M{"$each":addsales}}}

答案 1 :(得分:0)

addsales:= []Sales{Sales{
            FIRSTNAME : "chetan",
         LASTNAME : "kumar",
         AGE : 24,
          },
        }

    match := bson.M{"user.firstName" : "nepolean"}
    change := bson.M{"$push":bson.M{"user.$.sales":bson.M{"$each":addsales}}}


    err = c.Update(match,change)
    if err !=nil{
        panic(err)      
    }else{
    fmt.Println("success")
    }