查看MongoDB文档,您可以确保在应用程序运行时使用如下所示的命令为集合创建索引:
db.myCollection.ensureIndex({'my-col': 1}, {unique: true})
我在反应性mongo apis中找不到这样的东西。这样的事情存在吗?
答案 0 :(得分:7)
您可以从BSONCollection访问IndexManager:
collection.indexesManager.ensure(...)
有关详细信息,请参阅reactivemongo文档:
答案 1 :(得分:0)
从libraryDependencies开始==“ org.reactivemongo” %%“ play2-reactivemongo”%“ 0.20.3-play28”
第一个定义索引
val createdTSInx = Index(
key = Seq("createdTS" -> IndexType.Ascending),
name = Some("createdTSInx"),
unique = true,
background = false,
sparse = false,
expireAfterSeconds = None,
storageEngine = None,
weights = None,
defaultLanguage = None,
languageOverride = None,
textIndexVersion = None,
sphereIndexVersion = None,
bits = None,
min = None,
max = None,
bucketSize = None,
collation = None,
wildcardProjection = None,
version = None,
partialFilter = None,
options = BSONDocument.empty)
//创建
collection.indexesManager.create(createdTSInx)
//以确保
collection.indexesManager.ensure(createdTSInx)