为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 golang 中使用 $unwind?

如何在 golang 中使用 $unwind?

Go
拉丁的传说 2022-01-10 14:47:55
我想要结果,因为 mongo shell 提供给我。在 mongo shell 中,数据是这样的:db.user.aggregate([{$unwind:"$user"}]).pretty()    {    "_id" : ObjectId("57307906f051147d5317984e"),    "user" : {        "firstName" : "chetan",        "lastName" : "kumar",        "age" : 23    },    "sales" : [        {            "firstName" : "ashu",            "lastName" : "jha",            "age" : 27        }    ]}{    "_id" : ObjectId("57307906f051147d5317984e"),    "user" : {        "firstName" : "nepolean",        "lastName" : "dang",        "age" : 26    },    "sales" : [        {            "firstName" : "ashu",            "lastName" : "jha",            "age" : 27        }    ]}但是在去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")        }结果是这样的: [{"_id":"57307906f051147d5317984e","user":null,"sales":[{"firstName":"ashu","lastName":"jha","age":27}]},{"_id":"57307906f051147d5317984e","user":null,"sales":[{"firstName":"ashu","lastName":"jha","age":27}]}]        但它表明"user": null,我希望得到 mongo shell 提供的结果。
查看完整描述

1 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

因为映射是无效的,在$unwindon之后$user,你应该期望每个结果只包含一个用户,因此,User不应该是array.


type Details struct{

    ID  bson.ObjectId   `json:"_id" bson:"_id"`

    USER    []User      `json:"user" bson:"user"`

    SALES   []Sales     `json:"sales" bson:"sales"`

}

应改为:


type Details struct{

    ID  bson.ObjectId   `json:"_id" bson:"_id"`

    USER    User      `json:"user" bson:"user"`

    SALES   []Sales     `json:"sales" bson:"sales"`

}

由于您可能需要两种类型的响应,一种是正常响应,另一种$unwind是当前响应,您可以为其创建另一种类型:


type UnwindDetails struct{

    ID  bson.ObjectId   `json:"_id" bson:"_id"`

    USER    User      `json:"user" bson:"user"`

    SALES   []Sales     `json:"sales" bson:"sales"`

}

保持你原来的样子 type


type Details struct{

    ID  bson.ObjectId   `json:"_id" bson:"_id"`

    USER    []User      `json:"user" bson:"user"`

    SALES   []Sales     `json:"sales" bson:"sales"`

}

因此,您必须将result变量的类型修改为:


var result []UnwindDetails


查看完整回答
反对 回复 2022-01-10
  • 1 回答
  • 0 关注
  • 184 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号