英文:
How to convert the following aggregate query to bson and execute in golang
问题
这是我想从代码中运行的查询。
db.apnSummary.aggregate([{"$match":{"mid":{"$in":["81"]}}},{"$group":{"_id":"$mid","clicked":{"$sum":"$open"},"delivered":{"$sum":"$delivered"},"failed":{"$sum":"$failed"},"freqDrop":{"$sum":"$freqDrop"},"published":{"$sum":"$published"},"sent":{"$sum":"$sent"}}}]);
从代码中准备的查询:-
[{"$match":{"date":{},"mid":{"$in":[81]}}},{"$group":{"_id":"$mid","clicked":{"$sum":"$open"},"delivered":{"$sum":"$delivered"},"failed":{"$sum":"$failed"},"freqDrop":{"$sum":"$freqDrop"},"published":{"$sum":"$published"},"sent":{"$sum":"$sent"}}}]
当我执行这个查询时,我得到了结果,但是当我从代码中执行查询时,它没有给我提供结果,而是给出了空白数据。
我的代码如下:-
func GetApnDataFromMongo(data utils.Data, clientName string) (utils.Results, error) {
result := utils.Results{}
date := bson.M{}
messageId := bson.M{}
if len(data.MessageId) > 0 {
messageId = bson.M{"$in": convertToInt(data.MessageId)}
}
if data.DateFrom != "" && data.DateTo != "" {
date = bson.M{"$gte": data.DateFrom, "$lte": data.DateTo}
}
primary_collection := bson.M{"$match": bson.M{"mid": messageId, "date": date}}
group_collection := bson.M{"$group": bson.M{"_id": "$mid", "published": bson.M{"$sum": "$published"}, "sent": bson.M{"$sum": "$sent"}, "failed": bson.M{"$sum": "$failed"}, "freqDrop": bson.M{"$sum": "$freqDrop"}, "delivered": bson.M{"$sum": "$delivered"}, "clicked": bson.M{"$sum": "$open"}}}
fullquery := []bson.M{primary_collection, group_collection}
jsonString, _ := json.Marshal(fullquery)
logrus.Info(fmt.Sprintf("Final query prepared is as following %+v", string(jsonString)))
ctx := context.Background()
conn, err := utils.GetMongoConnection()
if err != nil {
logrus.Errorf("Error connecting to mongo %+v", err)
return result, err
}
logrus.Info("clientName %+v", clientName)
resultData, err := conn.Database(clientName).Collection("apnSummary").Aggregate(ctx, mongo.Pipeline(primary_collection, group_collection))
if err != nil {
logrus.Errorf("Error querying %+v", err)
return result, err
}
mongodata := []utils.MonogResponse{}
resultData.All(ctx, &mongodata)
fmt.Println(mongodata)
result = PrepareMongoResponse(mongodata)
return result, nil
}
func PrepareMongoResponse(data []utils.MonogResponse) utils.Results {
result := utils.Results{}
for _, res := range data {
result.Failure.Mid = append(result.Failure.Mid, res.Mid)
result.Delivered.Mid = append(result.Delivered.Mid, res.Mid)
result.Frequency.Mid = append(result.Frequency.Mid, res.Mid)
result.Sent.Mid = append(result.Sent.Mid, res.Mid)
result.Click.Mid = append(result.Click.Mid, res.Mid)
result.Failure.Count = append(result.Failure.Count, res.Failed)
result.Delivered.Count = append(result.Delivered.Count, res.Delivered)
result.Frequency.Count = append(result.Frequency.Count, res.FreqDrop)
result.Sent.Count = append(result.Sent.Count, res.Sent)
result.Click.Count = append(result.Click.Count, res.Clicked)
}
return result
}
func convertToInt(data []string) []int {
intData := []int{}
for _, dt := range data {
intV, _ := strconv.Atoi(dt)
intData = append(intData, intV)
}
return intData
}
<details>
<summary>英文:</summary>
This is the following query that I want to run it from code.
db.apnSummary.aggregate([{"$match":{"mid":{"$in":["81"]}}},{"$group":{"_id":"$mid","clicked":{"$sum":"$open"},"delivered":{"$sum":"$delivered"},"failed":{"$sum":"$failed"},"freqDrop":{"$sum":"$freqDrop"},"published":{"$sum":"$published"},"sent":{"$sum":"$sent"}}}]);
Query prepared from code :-
```
[{"$match":{"date":{},"mid":{"$in":[81]}}},{"$group":{"_id":"$mid","clicked":{"$sum":"$open"},"delivered":{"$sum":"$delivered"},"failed":{"$sum":"$failed"},"freqDrop":{"$sum":"$freqDrop"},"published":{"$sum":"$published"},"sent":{"$sum":"$sent"}}}]
```
when I execute this query I get the results but when I do the query from code it does not provide me result gives blank data.
my code is as following:-
func GetApnDataFromMongo(data utils.Data, clientName string) (utils.Results, error) {
result := utils.Results{}
date := bson.M{}
messageId := bson.M{}
if len(data.MessageId) > 0 {
messageId = bson.M{"$in": convertToInt(data.MessageId)}
}
if data.DateFrom != "" && data.DateTo != "" {
date = bson.M{"$gte": data.DateFrom, "$lte": data.DateTo}
}
primary_collection := bson.M{"$match": bson.M{"mid": messageId, "date": date}}
group_collection := bson.M{"$group": bson.M{"_id": "$mid", "published": bson.M{"$sum": "$published"}, "sent": bson.M{"$sum": "$sent"}, "failed": bson.M{"$sum": "$failed"}, "freqDrop": bson.M{"$sum": "$freqDrop"}, "delivered": bson.M{"$sum": "$delivered"}, "clicked": bson.M{"$sum": "$open"}}}
fullquery := []bson.M{primary_collection, group_collection}
jsonString, _ := json.Marshal(fullquery)
logrus.Info(fmt.Sprintf("Final query prepared is as following %+v", string(jsonString)))
ctx := context.Background()
conn, err := utils.GetMongoConnection()
if err != nil {
logrus.Errorf("Error connecting to mongo %+v", err)
return result, err
}
logrus.Info("clientName %+v", clientName)
resultData, err := conn.Database(clientName).Collection("apnSummary").Aggregate(ctx, mongo.Pipeline(primary_collection, group_collection))
if err != nil {
logrus.Errorf("Error querying %+v", err)
return result, err
}
mongodata := []utils.MonogResponse{}
resultData.All(ctx, &mongodata)
fmt.Println(mongodata)
result = PrepareMongoResponse(mongodata)
return result, nil
}
func PrepareMongoResponse(data []utils.MonogResponse) utils.Results {
result := utils.Results{}
for _, res := range data {
result.Failure.Mid = append(result.Failure.Mid, res.Mid)
result.Delivered.Mid = append(result.Delivered.Mid, res.Mid)
result.Frequency.Mid = append(result.Frequency.Mid, res.Mid)
result.Sent.Mid = append(result.Sent.Mid, res.Mid)
result.Click.Mid = append(result.Click.Mid, res.Mid)
result.Failure.Count = append(result.Failure.Count, res.Failed)
result.Delivered.Count = append(result.Delivered.Count, res.Delivered)
result.Frequency.Count = append(result.Frequency.Count, res.FreqDrop)
result.Sent.Count = append(result.Sent.Count, res.Sent)
result.Click.Count = append(result.Click.Count, res.Clicked)
}
return result
}
func convertToInt(data []string) []int {
intData := []int{}
for _, dt := range data {
intV, _ := strconv.Atoi(dt)
intData = append(intData, intV)
}
return intData
}
答案1
得分: 1
如果date
过滤器缺失,不要添加一个"date": {}
过滤器,该过滤器会筛选出"date"
为空对象的文档,显然在你的情况下不适用。如果没有日期过滤器,请在$match
阶段中省略date
!
对于messageID
和所有其他过滤器也是同样的处理方式。
你可以这样做:
matchDoc := bson.M{}
if len(data.MessageId) > 0 {
matchDoc["mid"] = bson.M{"$in": convertToInt(data.MessageId)}
}
if data.DateFrom != "" && data.DateTo != "" {
matchDoc["date"] = bson.M{"$gte": data.DateFrom, "$lte": data.DateTo}
}
primary_collection := bson.M{"$match": matchDoc}
注意:你的原始查询有效是因为你在其中省略了date:{}
过滤器!
英文:
If date
filter is missing, do not add a "date": {}
filter, that filters for documents where "date"
is an empty object, which obviously don't hold in your case. Leave out date
from the $match
stage if you don't have a date filter!
Same goes for messageID
and all other filters.
You may do it like this:
matchDoc := bson.M{}
if len(data.MessageId) > 0 {
matchDoc["mid"] = bson.M{"$in": convertToInt(data.MessageId)}
}
if data.DateFrom != "" && data.DateTo != "" {
matchDoc["date"] = bson.M{"$gte": data.DateFrom, "$lte": data.DateTo}
}
primary_collection := bson.M{"$match": matchDoc}
Note: your original query works because you left out the date:{}
filter from it!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论