douzi1350 2016-05-24 07:53
浏览 66
已采纳

如何在Golang中为嵌套数组推送$?

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

    /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条回答 默认 最新

  • douyi02577 2016-05-24 08:49
    关注

    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}}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘