如何在Golang中使用$ unwind?

Chetan Kumar:

我希望得到的结果是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")
        }
        defer session.Close()


        session.SetMode(mgo.Monotonic, true)

        c := session.DB("userdb").C("user")


       var result []Details


    o1 := bson.M{
        "$unwind":"$user",
    }


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

}

结果是这样的:

 [{"_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提供的结果。

ch33s:

由于映射无效,因此,$unwind开启$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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 Swift 3 中使用 unwind(for: UIStoryboardSegue,comingViewController: UIViewController)?

如何在密码查询中使用 UNWIND 后的条件

mongoDB:如何撤消$ unwind

在Golang和MongoDB中使用$ lookup和$ unwind的请求很慢

在多个文档上使用$ unwind

如何在同一个 mongodb 查询中使用 $unwind 然后 $group

如何使用$ unwind保持文档聚合

如何在使用cypher与UNWIND进行迭代时删除关系

如何以编程方式执行Unwind segue?

如何以编程方式创建Unwind segue

在嵌套数组上使用$ unwind的后果?

选择不使用$ unwind(猫鼬聚合)

MongoDB:使用$ unwind进入$ lookup管道

在聚合框架mongodb中使用$ unwind和$ text

在mongodb中的多个嵌套数组中使用unwind

如何在 $lookup 后反转 $unwind 或重新组装?

什么是Unwind segues,您如何使用它们?

什么是__aeabi_unwind_cpp_pr1',如何避免呢?

将-fno-unwind-tables与-fno-exceptions结合使用

与UISearchController一起使用unwind segue时出错

使用 unwind segue 返回上一页

在我创建的列表上使用UNWIND返回多个值(密码)

使用 $unwind 获取空数组作为聚合结果

MongoDB 使用 group 和 unwind 计数出现次数

如何在 Unwind segue 中切换选项卡式视图

我如何在postgres JSONB中实现mongo“ unwind”?(平面嵌套数组)

当您有多个ViewController调用它时,如何使用unwind segue

使用$ unwind时可以避免两次使用相同的$ match条件吗?

使用panic :: catch_unwind时抑制Rust中的panic输出