1

I use the following struct to store data in mongodb collection

type StatisticTransactions struct {
    Timestamp                  uint64           `bson:"timestamp" json:"timestamp"`
    UID                        uint32           `bson:"uid" json:"uid"`
    AvgFullnessInPercents      float64          `bson:"avgFullnessInPercents" json:"avgFullnessInPercents"`
    SummaryTransactionCounters Transactions     `bson:"summaryTransactionCounters" json:"summaryTransactionCounters"`
}
type Transactions struct {
    Total            uint64 `bson:"total" json:"total"`
    Success          uint64 `bson:"success" json:"success"`
    FailOneDirection uint64 `bson:"failOneDirection" json:"failOneDirection"`
}

and this is the formula for AvgFullnessInPercents

AvgFullnessInPercents = (float64(SummaryTransactionCounters.Success)/float64(SummaryTransactionCounters.Total))*100

There isn't problem to insert data for the first time, but i don't understand how to update data. For example:

We have in mongodb collection:

{
    Timestamp:             12345,
    UID:                   1,
    AvgFullnessInPercents: 10,
    SummaryTransactionCounters: Transactions{
        Total:            1000,
        Success:          100,
        FailOneDirection: 256,
    }
}

and we want to update them with

newData := StatisticTcapCountries{
    Timestamp:                  time.Now().UTC(),
    UID:                        1,
    AvgFullnessInPercents:      25,
    SummaryTransactionCounters: Transactions{
        Total:            100,
        Success:          25,
        FailOneDirection: 37,
    }
}

This is my update code:

_, err = c.ClientTarget.
                Database(dbTransactions).
                Collection(colTransactions).
                UpdateOne(
                    ctx,
                    bson.D{
                        {Key: colTransactionsFieldUID, Value: newData.UID},
                    },
                    bson.D{
                        {
                            Key: "$setOnInsert",
                            Value: bson.D{
                                {Key: colTransactionsFieldTimestamp, Value: newData.Timestamp},
                                {Key: colTransactionsFieldAvgFullnessInPercents, Value: newData.AvgFullnessInPercents},
                                {Key: colTransactionsFieldSummaryTransactionCounters, Value: newData.SummaryTransactionCounters},
                            },
                        },
                        {
                            Key: "$set",
                            Value: bson.D{
                                {Key: colTransactionsFieldTimestamp, Value: newData.Timestamp},
                            },
                        },
                        // increase fields of SummaryTransactionCounters
                        {
                            Key: "$inc",
                            Value: bson.D{
                                {Key: colTransactionsFieldSummaryTransactionCountersTotal, Value: newData.SummaryTransactionCounters.Total},
                                {Key: colTransactionsFieldSummaryTransactionCountersSuccess, Value: newData.SummaryTransactionCounters.Success},
                                {Key: colTransactionsFieldSummaryTransactionCountersFailOneDirection, Value: newData.SummaryTransactionCounters.FailOneDirection},
                            },
                        },
                        {
                            Key: "$set",
                            Value: bson.D{
                                {
                                    Key:   colTransactionsFieldAvgFullnessInPercents,
                                    Value: ?
                                },
                            },
                        },
                    },
                    options.Update().SetUpsert(true),
                )

What should i write for colTransactionsFieldAvgFullnessInPercents field to update it with formula above using updated values of colTransactionsFieldSummaryTransactionCountersSuccess and colTransactionsFieldSummaryTransactionCountersTotal fields?

Yes, it's possible to:

  1. get data from mongodb
  2. update Trancation counters and calculate avgPercents
  3. put updated data into mongodb again

But it would be great to do it in one Update clause.

2
  • You can use Updates with Aggregation Pipeline feature - this allows using the document field values to be used in calculations and update the new values to the same document. An example post: stackoverflow.com/questions/69831670/… Commented Feb 15, 2022 at 9:14
  • I understand and i've tried but how would i do that with go.mongodb.org driver syntax? Commented Feb 16, 2022 at 12:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.