Question

Looking at the MongoDB documentation it looks like you can ensure an index is created for a collection at application runtime using a command that looks something like this:

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

I can't find anything like this in the reactive mongo apis. Does such a thing exist?

Was it helpful?

Solution

You can access the IndexManager from your BSONCollection:

collection.indexesManager.ensure(...)

See reactivemongo docs for details:

  • 0.9 (as of Jan 2019 - gives 404)
  • 0.1x (as of Jan 2019 - current version)

OTHER TIPS

As of libraryDependencies += "org.reactivemongo" %% "play2-reactivemongo" % "0.20.3-play28"

1st define the index

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)

// to create

  collection.indexesManager.create(createdTSInx)

//to ensure

  collection.indexesManager.ensure(createdTSInx)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top