Mongodb索引管理

89 阅读1分钟

获取所有索引以及创建单字段索引

1.db.collectionName.getIndexes(),获取索引

2.db.collectionName.createIndex({userid:1})将userid以升序的形式作为索引


3.创建符合索引

db.collectionName.createIndex({userid:1,nickname:-1})以userid为升序且nickname为降序的符合索引


4.索引的移除

db.collectionName.dropIndex(index)

db.comment.dropIndex({userid:1})

还可以以name作为索引去删除

db.comment.dropIndex(indexName)

db.comment.dropIndex("userid_1_nickname_-1")