drydaenth257216154 2015-12-22 22:52
浏览 148
已采纳

golang mgo TTL指数

how do I create a TTL (time to live) index with golang and mongodb? This is how I'm trying to do it currently:

sessionTTL := mgo.Index{
    Key:         []string{"created"},
    Unique:      false,
    DropDups:    false,
    Background:  true,
    ExpireAfter: session_expire} // session_expire is a time.Duration

if err := db.C("session").EnsureIndex(sessionTTL); err != nil {
    panic(err)
}

But if I look it up using:

db.session.getIndexes()

session_expire is set to 5*time.Second. The field "created" in the document is set to current date using time.Now(), so I expected the documents the be deleted after 5 seconds.

  • 写回答

2条回答 默认 最新

  • doufei2328 2015-12-23 01:32
    关注

    So the issue was that I had to drop the collection. The index existed already so it was not recreated with the expiration constraint.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?