我知道我可以在MongoDB中设置TTL:
db.ttl_collection.ensureIndex( { "Date": 1 }, { expireAfterSeconds: 10 } ) 我知道我可以在Reactivemongo中使用Scala来确保索引
collection.indexesManager.ensure(index)但是如何从代码中设置reactivemongo中的TTL集合呢?或者,有没有其他方法可以在Scala中使用reactivemongo在Mongo中创建过期记录?
发布于 2014-11-24 01:31:30
我终于发现了。这并不是真正明确的方法,但似乎是有效的:
collection.indexesManager.ensure(Index(Seq(("Date", IndexType(BSONInteger(1)))), Some("expireAfterSeconds"), false, false, false, false, None, BSONDocument( "expireAfterSeconds" -> 0 )这样,这个集合中包含expireAfterSeconds: BSONDateTime的每个对象都将在指定的日期之后过期,但我甚至不知道这些布尔值负责什么。
发布于 2015-09-12 21:10:18
在我的项目中,我们有这个函数
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))https://stackoverflow.com/questions/27091061
复制相似问题