2014-11-23 82 views
0

我知道我可以設置TTL MongoDB中與Reactivemongo TTL集合

db.ttl_collection.ensureIndex({ "Date": 1 }, { expireAfterSeconds: 10 }) 

,我知道我可以保證使用Scala指數Reactivemongo與

collection.indexesManager.ensure(index) 

但我怎麼能設置在reactivemongo TTL收藏從代碼? 或者有沒有其他方法可以在Mongo中使用Scala中的reactivemongo過期記錄?

回答

1

我終於找到了。這不是一個真正清晰的方式來做到這一點,但似乎工作:

collection.indexesManager.ensure(Index(Seq(("Date", IndexType(BSONInteger(1)))), Some("expireAfterSeconds"), false, false, false, false, None, BSONDocument("expireAfterSeconds" -> 0) 

這種方式與expireAfterSeconds每個對象:指定日期的 後BSONDateTime在此集合就將到期,但我甚至不知道這些布爾負責對於。

+0

我猜 「日期」是文檔中的字段名稱,對嗎?你需要做其他事情才能使其工作?我在我的代碼中添加了這個,並且文檔沒有過期。這應該在初始化時還是在每次插入之前調用? – redwulf 2016-03-04 09:43:15

0

在我的項目,我們有這個功能

def ensureIndex(
       key: List[(String, IndexType)], 
       name: Option[String] = None, 
       unique: Boolean = false, 
       background: Boolean = false, 
       dropDups: Boolean = false, 
       sparse: Boolean = false, 
       version: Option[Int] = None, 
       options: BSONDocument = BSONDocument()) = { 

    val index = Index(key, name, unique, background, dropDups, sparse, version, options) 
    log.info(s"Ensuring index: $index") 
    collection.indexesManager.ensure(index) 
} 

我用它像TTL指標如下($doc來自BSON DSL):

ensureIndex(List("lastModifiedOn" -> IndexType.Ascending), options = $doc("expireAfterSeconds" -> 30))