Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to $push in golang for nested array?

Tags:

mongodb

go

mgo

I tried to push some data in nested array using $push. Here is my json file

{
    "_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
        }
    ],

}

Now here is my golang code which I tried here to execute

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))

}

Here, is my OutPut after executing

/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
like image 753
Chetan Kumar Avatar asked May 24 '16 07:05

Chetan Kumar


1 Answers

You are pushing an array of Sales with syntax for a single item.
It should be either a single Sales object to push:

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

or use $each modifier to push multiple items:

addsales:= []Sales{Sales{
    FIRSTNAME : "chetan",
    LASTNAME : "kumar",
    AGE : 24,
    },
}
change := bson.M{"$push":bson.M{"user.$.sales":bson.M{"$each":addsales}}}
like image 111
Alex Blex Avatar answered Oct 11 '22 10:10

Alex Blex