I want to implement record_nbr in mongo collection. I have searched Stackoverflow and there doesn't seems any straight forward way have a field in document which depicts the sequence number of this particular document in collection.
The code below is good to implement counter within document of collection but how to implement document counter number in collection?
opts := options.Update().SetUpsert(true)
filter := bson.D{primitive.E{Key: "_id", Value: TransactionID}}
update := bson.D{{"$inc", bson.D{{"record_number", 1}}}}
_, err = db.Collection("transaction").UpdateOne(context.TODO(), filter, update, opts)
expected behaviour required
[{
"_id": "61f81309936f24de43b4e799",
"record_nbr": 2
},
{
"_id": "61f81309936f24de43b4e0j8",
"record_nbr": 3
},
{
"_id": "f8f24de43b4e0j81309936op",
"record_nbr": 4
}]