4

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?

Jordan
  • 1,599
  • 4
  • 26
  • 42

2 Answers2

7

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)
J0HN
  • 26,063
  • 5
  • 54
  • 85
vptheron
  • 7,426
  • 25
  • 34
0

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)
swapnil shashank
  • 877
  • 8
  • 11