4

查看 MongoDB 文档,您可以确保在应用程序运行时使用如下命令为集合创建索引:

db.myCollection.ensureIndex({'my-col': 1}, {unique: true})

我在反应式 mongo api 中找不到类似的东西。这样的事情存在吗?

4

2 回答 2

7

您可以从 BSONCollection 访问 IndexManager:

collection.indexesManager.ensure(...)

有关详细信息,请参阅 reactivemongo 文档:

  • 0.9(截至 2019 年 1 月 - 给出 404)
  • 0.1x(截至 2019 年 1 月 - 当前版本)
于 2014-01-25T18:24:50.363 回答
0

截至 libraryDependencies += "org.reactivemongo" %% "play2-reactivemongo" % "0.20.3-play28"

1st定义索引

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)
于 2020-07-13T06:42:40.227 回答