Yanhui Zhou
Yanhui Zhou

Reputation: 898

MongoDb index already exists

When I use ensureIndex, I got a message,

"all indexes already exist".

But there is nothing, in getIndexes(). Why? Can somebody help me?

mongos> db.UserBase.ensureIndex({"userId":1}, {"unique":true})
{
    "raw" : {
        "shard2/192.168.2.69:27024,192.168.254.107:27024,192.168.254.108:27024" : {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 2,
            "numIndexesAfter" : 2,
            "note" : "all indexes already exist",
            "ok" : 1,
            "$gleStats" : {
                "lastOpTime" : Timestamp(1468049378, 96),
                "electionId" : ObjectId("7fffffff0000000000000014")
            }
        }
    },
    "ok" : 1
}
mongos> db.UserBase.getIndexes()
[ ]

Upvotes: 2

Views: 4076

Answers (2)

Tan Kim Loong
Tan Kim Loong

Reputation: 1035

If you're using MongoDB higher than version 3.0.0, you might want to consider using db.UserBase.createIndex({"userId":1}, {"unique":true}) instead. ensureIndex() is deprecated.

Source: https://docs.mongodb.com/manual/reference/method/db.collection.ensureIndex/

Upvotes: 3

Ely
Ely

Reputation: 11152

Maybe this answers your question?

Considerations

Changed in version 3.0.0.

For MongoDB 3.0 deployments using the WiredTiger storage engine, if you run db.collection.getIndexes() from a version of the mongo shell before 3.0 or a version of the driver prior to 3.0 compatible version, db.collection.getIndexes() will return no data, even if there are existing indexes. For more information, see WiredTiger and Driver Version Compatibility.

Try to use db.collection.createIndex().

Upvotes: 3

Related Questions