0

How do I set TTL indexes in a document using "mongo-go-driver"?

suppose here is my document model:

type Session struct {
    ID           primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
    AccessToken  string             `json:"access_token" bson:"access_token"`
    ExpireOn     int64              `json:"expire_on" bson:"expire_on"`
}

and here I am inserting new document:

sessionCollection := database.Collection(COLLECTION_SESSION)

_, err = sessionCollection.InsertOne(context.TODO(), s)
if err != nil {
    responses.RespondWithERROR(w, http.StatusInternalServerError, err)
    return
}

Now I want that this session will be removed after a few times.

I found the way to do that in documentation Expire Data from Collections by Setting TTL but can't figure out How to do the same thing in go using mongo-go-driver.

Abid
  • 477
  • 1
  • 4
  • 15

0 Answers0