Home > OS >  How to convert the following aggregate query to bson and execute in golang
How to convert the following aggregate query to bson and execute in golang

Time:08-06

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
}

CodePudding user response:

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!

  • Related