Suresh Payankannur

Wednesday, April 23, 2014

MongoDB and Spring Data: Compound Unique Index

Adding compound, unique index with MongoDB can be done as follows:
db.collection.ensureIndex( { column-1 : 1, column-2 : 1 }, { unique : true } )
To achieve the same using Spring Data, use the following annotations:
@CompoundIndexes({
        @CompoundIndex(name = "my_index_name",
                       unique = true,
                       def = "{'column-1' : 1, 'column-2' : 1}")
})


1 comment:

  1. I want to create index with "expireAfterSeconds" property. what syntax should be used?

    ReplyDelete

Blog Archive

Scroll To Top