簡體   English   中英

golang中的mongodb聚合查詢

[英]mongodb aggregation query in golang

我能夠運行以下 mongoDB 聚合查詢並在 Mongo DB 中獲得所需的結果。 當我在 Golang 中運行聚合查詢時,我想得到相同的結果。 但是,除了查詢中顯示空字符串值的兩個字段(即 displayName、msId)外,我按預期獲取了所有字段值(即持續時間、totalTime)。 它應該包含像 Mongo DB 查詢結果中的非空字符串值。 我在 golang 查詢中缺少什么?

mongoDB聚合查詢-

db.getCollection("db").aggregate(
    [
        { 
            "$match" : { 
                "attendanceDate" : "07/26/2022"
            }
        }, 
        { 
            "$unwind" : "$history"
        }, 
        { 
            "$set" : { 
                "timeDiff" : { 
                    "$divide" : [
                        { 
                            "$subtract" : [
                                "$history.endTime", 
                                "$history.startTime"
                            ]
                        }, 
                        60000.0
                    ]
                }
            }
        }, 
        { 
            "$group" : { 
                "_id" : { 
                    "status" : "$history.status", 
                    "displayName" : "$displayName", 
                    "msId" : "$msId"
                }, 
                "duration" : { 
                    "$sum" : "$timeDiff"
                }
            }
        }, 
        { 
            "$group" : { 
                "_id" : { 
                    "displayName" : "$_id.displayName", 
                    "msId" : "$_id.msId"
                }, 
                "durations" : { 
                    "$push" : { 
                        "key" : "$_id.status", 
                        "value" : "$duration"
                    }
                }
            }
        }, 
        { 
            "$addFields" : { 
                "displayName" : "$_id.displayName", 
                "msId" : "$_id.msId", 
                "totalTime" : { 
                    "$sum" : "$durations.value"
                }
            }
        }
    ]
);

上面在 Mongo 中查詢的結果 -

{ 
    "_id" : {
        "displayName" : "John, Doe", 
        "msId" : "abcd"
    }, 
    "durations" : [
        {
            "key" : "Other", 
            "value" : NumberInt(0)
        }, 
        {
            "key" : "Break", 
            "value" : 1.9216166666666668
        }, 
        {
            "key" : "Lunch", 
            "value" : 0.9956666666666667
        }, 
        {
            "key" : "In", 
            "value" : 9.3131
        }, 
        {
            "key" : "Training", 
            "value" : 0.9886666666666667
        }
    ], 
    "displayName" : "John, Doe", 
    "msId" : "abcd", 
    "totalTime" : 13.219050000000001
}

Golang 代碼 -

type AttendanceAggregate struct {
    DisplayName string                    `json:"displayName" bson:"displayName"`
    ID          string                    `json:"msId" bson:"msId"`
    TotalTime   float64                   `json:"totalTime" bson:"totalTime"`
    Duration    []AttendanceAggregateItem `json:"durations" bson:"durations"`
}

type AttendanceAggregateItem struct {
    Key   string  `json:"key,omitempty" bson:"key,omitempty"`
    Value float64 `json:"value,omitempty" bson:"value,omitempty"`
}


 func (r *repo) Find() ([]domain.AttendanceAggregate, error) {

    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    var attendances []domain.AttendanceAggregate

    pipeline := []bson.M{
        {
            "$match": bson.M{
                "attendanceDate": "07/26/2022",
            },
        },
        {
            "$unwind": "$history",
        },
        {
            "$set": bson.M{
                "timeDiff": bson.M{
                    "$subtract": bson.A{
                        "$history.endTime",
                        "$history.startTime",
                    },
                },
            },
        },
        {
            "$set": bson.M{
                "timeDiff": bson.M{
                    "$divide": bson.A{
                        "$timeDiff",
                        60000.0,
                    },
                },
            },
        },
        {
            "$group": bson.M{
                "_id": bson.M{
                    "status":      "$history.status",
                    "displayName": "$displayName",
                    "msId":        "$msId",
                },
                "duration": bson.M{
                    "$sum": "$timeDiff",
                },
            },
        },
        {
            "$group": bson.M{
                "_id": bson.M{
                    "displayName": "$displayName",
                    "msId":        "$msId",
                },
                "durations": bson.M{
                    "$push": bson.M{
                        "key":   "$_id.status",
                        "value": "$duration",
                    },
                },
            },
        },
        {
            "$addFields": bson.M{
                "displayName": "$_id.displayName",
                "msId":        "$_id.msId",
                "totalTime": bson.M{
                    "$sum": "$durations.value",
                },
            },
        },
    }

    cur, err := r.Db.Collection("db").Aggregate(ctx, pipeline)
    defer cur.Close(ctx)
    if err != nil {

        return attendances, err
    }

    for cur.Next(ctx) {
        var attendance domain.AttendanceAggregate

        err := cur.Decode(&attendance)
        if err != nil {
            return attendances, err
        }
     
        attendances = append(attendances, attendance)
    }

    if err = cur.Err(); err != nil {
        return attendances, err
    }

    return attendances, nil
}

golang查詢結果截圖-golang查詢結果截圖

Go 中的最后一個$group階段缺少displayNamemsId字段中的_id前綴:

代替:

    {
        "$group": bson.M{
            "_id": bson.M{
                "displayName": "$displayName",
                "msId":        "$msId",
            },
            "durations": bson.M{
                "$push": bson.M{
                    "key":   "$_id.status",
                    "value": "$duration",
                },
            },
        },
    },

利用:

    {
        "$group": bson.M{
            "_id": bson.M{
                "displayName": "$_id.displayName",
                "msId":        "$_id.msId",
            },
            "durations": bson.M{
                "$push": bson.M{
                    "key":   "$_id.status",
                    "value": "$duration",
                },
            },
        },
    },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM