scalareactivemongo

Creating a unique index for a collection in reactivemongo


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?


Solution

  • You can access the IndexManager from your BSONCollection:

    collection.indexesManager.ensure(...)
    

    See reactivemongo docs for details: