duanjian9148 2016-05-12 05:45
浏览 32

如何编写这些golang和mgo代码

My mongodb data is here

 db.user.aggregate([{$project:{"_id":1,"NoOfUsers":{"$size":"$user"},"NoOfSales":{"$size":"$sales"},"user":1,"sales":1}}]).pretty()
{
    "_id" : ObjectId("57307906f051147d5317984e"),
    "user" : [
        {
            "firstName" : "chetan",
            "lastName" : "kumar",
            "age" : 23
        },
        {
            "firstName" : "nepolean",
            "lastName" : "dang",
            "age" : 26
        },
        {
            "firstName" : "Raj",
            "lastname" : "kumar",
            "age" : 26
        }
    ],
    "sales" : [
        {
            "firstName" : "ashu",
            "lastName" : "jha",
            "age" : 27
        }
    ],
    "NoOfUsers" : 3,
    "NoOfSales" : 1
}

I try to write these code in golang , here is my golang code

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{
    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"`
    SALES   []Sales     `json:"sales" bson:"sales"`
}
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")


       var result []Details


    o1 := bson.M{"$project":bson.M{"_id":1,"NoOfUser":bson.M{"$size":"$user"},"user":1,"NoOfSales":bson.M{"$size":"$sales"},"sales":1,},}

        operations := []bson.M{o1}
    pipe := c.Pipe(operations)
    err = pipe.All(&result)
        if err != nil {
                log.Fatal(err)
        }
        res1B, _ := json.Marshal(result)
        fmt.Fprintf(w,string(res1B))
}

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

}

After executing this code my result is like this

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

Problem is ,it does not show the NoOfusers and NoOfsales ,So is this possible to print the same output as present in above mongodb result without using any extra struct field like NoOfusers or NoOfsales

  • 写回答

1条回答 默认 最新

  • drq61040 2016-05-13 01:46
    关注

    If you just want to print the result without any mapping from Details struct, then replace var result []Details with var result []bson.M.

    If you want to map into Details, then you need to add a struct for the two fields into Details.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line