在我的项目中,我们有这个功能
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))