[英]mongodb aggregation pipeline in golang for $divide $subtract
In golang, how do I write pipeline for the following mongodb aggregation query?在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"
},
"duration" : {
"$sum" : "$timeDiff"
}
}
},
{
"$group" : {
"_id" : "$_id.displayName",
"durations" : {
"$push" : {
"key" : "$_id.status",
"value" : "$duration"
}
}
}
}
],
JSON doc in mongodb ver 4.2 mongodb 版本 4.2 中的 JSON 文档
{
"_id" : ObjectId("62e01543666e8a64c2aeec56"),
"attendanceDate" : "07/26/2022",
"displayName" : "John, Doe",
"signInDate" : ISODate("2022-07-26T16:24:35.488+0000"),
"currentStatus" : "Other",
"currentStatusTime" : ISODate("2022-07-26T16:37:54.890+0000"),
"history" : [
{
"status" : "Other",
"startTime" : ISODate("2022-07-26T16:37:54.890+0000")
},
{
"status" : "In",
"startTime" : ISODate("2022-07-26T16:33:00.655+0000"),
"endTime" : ISODate("2022-07-26T16:37:54.890+0000")
},
{
"status" : "Training",
"startTime" : ISODate("2022-07-26T16:32:01.337+0000"),
"endTime" : ISODate("2022-07-26T16:33:00.657+0000")
},
{
"status" : "In",
"startTime" : ISODate("2022-07-26T16:31:00.764+0000"),
"endTime" : ISODate("2022-07-26T16:32:01.338+0000")
},
{
"status" : "Lunch",
"startTime" : ISODate("2022-07-26T16:30:01.025+0000"),
"endTime" : ISODate("2022-07-26T16:31:00.765+0000")
},
{
"status" : "In",
"startTime" : ISODate("2022-07-26T16:27:33.789+0000"),
"endTime" : ISODate("2022-07-26T16:30:01.026+0000")
},
{
"status" : "Break",
"startTime" : ISODate("2022-07-26T16:25:38.492+0000"),
"endTime" : ISODate("2022-07-26T16:27:33.789+0000")
},
{
"status" : "In",
"startTime" : ISODate("2022-07-26T16:24:41.753+0000"),
"endTime" : ISODate("2022-07-26T16:25:38.493+0000")
}
]
}
Using mongodb version 4.2, result from the aggregation query returns result as below使用 mongodb 版本 4.2,聚合查询返回结果如下
{
"_id" : "John, Doe",
"durations" : [
{
"key" : "Other",
"value" : NumberInt(0)
},
{
"key" : "Lunch",
"value" : 0.9956666666666667
},
{
"key" : "In",
"value" : 9.3131
},
{
"key" : "Training",
"value" : 0.9886666666666667
},
{
"key" : "Break",
"value" : 1.9216166666666668
}
]
}
In golang, got it working until $unwind pipeline.在 golang 中,让它一直工作到 $unwind 管道。 I'm unsure how to proceed further to $divide, $subract and get the same result as in aggregation query result.我不确定如何进一步进行 $divide、$subract 并获得与聚合查询结果相同的结果。
type Attendance struct {
ID primitive.ObjectID `json:"_id" bson:"_id"`
Date time.Time `json:"signInDate" bson:"signInDate"`
DisplayName string `json:"displayName" bson:"displayName"`
CurrentStatus string `json:"currentStatus,omitempty" bson:"currentStatus,omitempty"`
CurrentStatusTime time.Time `json:"currentStatusTime,omitempty" bson:"currentStatusTime,omitempty"`
History []AttendanceHistoryItem `json:"history" bson:"history"`
}
type AttendanceHistoryItem struct {
Status string `json:"status,omitempty" bson:"status,omitempty"`
StartTime time.Time `json:"startTime,omitempty" bson:"startTime,omitempty"`
EndTime time.Time `json:"endTime,omitempty" bson:"endTime,omitempty"`
}
func (r *repo) Find() ([]domain.Attendance, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var attendances []domain.Attendance
pipeline := []bson.M{
{
"$match": bson.M{
"attendanceDate": "07/26/2022",
},
},
{
"$unwind": "$history",
},
}
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.Attendance
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
}
@Chandan @钱丹
[syntax error screenshot for the query you posted][1]
[1]: https://i.stack.imgur.com/bnjL4.jpg
bson.A
can be used as array for mongo pipeline bson.A
可用作 mongo 管道的数组
query := []bson.M{
{
"$match" : bson.M{
"attendanceDate" : "07/26/2022",
},
},
{
"$unwind" : "$history",
},
{
"$set" : bson.M{
"timeDiff" : bson.M{
"$divide" : bson.A{
{
"$subtract" : bson.M{
"$history.endTime",
"$history.startTime",
},
},
60000.0,
},
}
},
},
{
"$group" : bson.M{
"_id" : bson.M{
"status" : "$history.status",
"displayName" : "$displayName",
},
"duration" : bson.M{
"$sum" : "$timeDiff",
},
},
},
{
"$group" : bson.M{
"_id" : "$_id.displayName",
"durations" : bson.M{
"$push" : bson.M{
"key" : "$_id.status",
"value" : "$duration",
},
},
},
},
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.