简体   繁体   English

如何编写这些golang和mgo代码

[英]How to write these golang and mgo code

My mongodb data is here 我的mongodb数据在这里

 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 我尝试在golang中编写这些代码,这是我的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{
    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 问题是,它没有显示NoOfusersNoOfsales ,因此可以打印与上面mongodb结果中相同的输出而不使用任何额外的结构字段,如NoOfusers或NoOfsales

If you just want to print the result without any mapping from Details struct, then replace var result []Details with var result []bson.M . 如果您只想在没有Details struct的任何映射的情况下打印结果,则将var result []Details替换为var result []bson.M

If you want to map into Details , then you need to add a struct for the two fields into Details . 如果要映射到“ Details ,则需要将两个字段的结构添加到“ Details

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM