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