Skip to content

Commit

Permalink
fix query length
Browse files Browse the repository at this point in the history
  • Loading branch information
bxy4543 committed Aug 1, 2023
1 parent 40babea commit 6f358c0
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions controllers/pkg/database/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ func (m *MongoDB) QueryBillingRecords(billingRecordQuery *accountv1.BillingRecor

pipelineAll := bson.A{
matchStage,
bson.D{primitive.E{Key: "$group", Value: bson.D{
primitive.E{Key: "_id", Value: nil},
primitive.E{Key: "result", Value: bson.D{primitive.E{Key: "$sum", Value: 1}}},
}}},
}

pipelineCountAndAmount := bson.A{
Expand Down Expand Up @@ -454,8 +458,16 @@ func (m *MongoDB) QueryBillingRecords(billingRecordQuery *accountv1.BillingRecor
if err != nil {
return fmt.Errorf("failed to execute aggregate all query: %w", err)
}
totalCount = cursorAll.RemainingBatchLength()
cursorAll.Close(ctx)
defer cursorAll.Close(ctx)
for cursorAll.Next(ctx) {
var result struct {
Result int64 `bson:"result"`
}
if err := cursorAll.Decode(&result); err != nil {
return fmt.Errorf("failed to decode query count record: %w", err)
}
totalCount = int(result.Result)
}

// 消费总金额Costs Executing the second pipeline for getting the total count, recharge and deduction amount
cursorCountAndAmount, err := billingColl.Aggregate(ctx, pipelineCountAndAmount)
Expand Down

0 comments on commit 6f358c0

Please sign in to comment.