Home > Mobile >  Finding Unique Indexes in MongoDB
Finding Unique Indexes in MongoDB

Time:12-29

How I can find all unique indexes in MongoDB?

The db.collection.getIndexes() function doesn't give any information about uniqueness.

CodePudding user response:

getIndexes() should work:

db.collection.createIndex({key: 1}, {unique: true})
    
db.collection.getIndexes()

[
    {
        "v" : 2,
        "key" : { "_id" : 1 },
        "name" : "_id_"
    },
    {
        "v" : 2,
        "key" : { "key" : 1 },
        "name" : "key_1",
        "unique" : true
    }
]

If the index is not unique then "unique": true is simply missing.

  • Related