如何使用golang在mongodb中使用$ lookup获取计数值?

时间:2018-04-25 09:41:45

标签: mongodb go mgo

使用聚合我正在使用golang加入两个mongodb集合。结果如下所示: -

输出: -

{
"response": {
    "code": 1,
    "api_status": 1,
    "message": "Success",
    "total_record": [
        {
            "_id": 1,
            "author_name": "mohit",
            "category": 232,
            "content": "This is the content",
            "date_time": 1524632713,
            "excerpt": "This is a  short text",
            "image": "pic.jpg",
            "resultField": [
                {
                    "_id": 6,
                    "comment": "this is a least comment",
                    "comment_on": 1524644601,
                    "email": "puneet@bookingkoala.com",
                    "name": "puneet",
                    "post_id": 1,
                    "reply_to": 1524644601,
                    "status": 1
                },
                {
                    "_id": 7,
                    "comment": "this is a least comment",
                    "comment_on": 1524647808,
                    "email": "puneet@bookingkoala.com",
                    "name": "puneet",
                    "post_id": 1,
                    "reply_to": 1524647808,
                    "status": 1
                }
            ],
            "status": 0,
            "tags": "this",
            "title": "how to do the code"
        },
        {
            "_id": 2,
            "author_name": "mohit",
            "category": 232,
            "content": "This is the content",
            "date_time": 1524632713,
            "excerpt": "This is a  short text",
            "image": "pic.jpg",
            "resultField": [
                {
                    "_id": 8,
                    "comment": "this is a least comment",
                    "comment_on": 1524648059,
                    "email": "puneet@bookingkoala.com",
                    "name": "puneet",
                    "post_id": 2,
                    "reply_to": 1524648059,
                    "status": 1
                }
            ],
            "status": 0,
            "tags": "this",
            "title": "how to do the code"
        },
        {
            "_id": 3,
            "author_name": "puneet",
            "category": 2,
            "content": "this is content",
            "date_time": 1524641086,
            "excerpt": "this is excerpt",
            "image": "pic.jpg",
            "resultField": [],
            "status": 1,
            "tags": "go",
            "title": "how to do the code"
        }
    ]
 }
}

输出由以下golang代码执行: -

mongoSession := config.ConnectDb()
collection := mongoSession.DB(config.Database).C(config.BlogCollection)
pipeline := []bson.M{
    bson.M{"$match": bson.M{}},
    bson.M{"$lookup": bson.M{"from" : "comment", "localField" : "_id", "foreignField": "post_id","as": "resultField" }},
}
fmt.Println(pipeline)
pipe := collection.Pipe(pipeline)
resp := []bson.M{}
err = pipe.All(&resp)
if err != nil {
    fmt.Println("Errored: %#v \n", err)
}
fmt.Println(resp)
if err != nil {
    response = ResponseControllerList{
        config.FailureCode,
        config.FailureFlag,
        config.FailureMsg,
        nil,
        nil,
    }
} else {
    response = ResponseControllerList{
        config.SuccessFlag,
        config.SuccessFlag,
        config.SuccessMsg,
        nil,
        resp,
    }
}

问题是: - 我只需要count data而不是 data。输出中的平均值如上所示有resultField,其中显示了数据,但我只需要count值,如: - "resultField":[2]但它显示的是数据。如何获取输出中数据的计数值。提前谢谢。

1 个答案:

答案 0 :(得分:0)

因此,您的聚合实际上会返回comment字段中的所有resultField个文档,其中隐含地包含结果的数量,它是一个切片,您可以使用内置函数检查Go中的长度len()功能。

由于您只需要长度(comment个文档的数量),这就是您只想检索此数组大小的原因。为此,您可以使用$addFields阶段将resultField数组替换为数字,即该数组的长度。

pipe := c.Pipe([]bson.M{
    {
        "$lookup": bson.M{
            "from":         "comment",
            "localField":   "_id",
            "foreignField": "post_id",
            "as":           "resultField",
        },
    },
    {
        "$addFields": bson.M{
            "resultField": bson.M{"$size": "$resultField"},
        },
    },
})

请注意,$addFields阶段等同于$project阶段,该阶段明确指定输入文档中的所有现有字段并添加新字段。仅在MongoDB版本3.4之后可用。