第一、创建索引
索引的设置(先选择数据库,再选择集合:collection为集合)
查看索引
db.collection.getIndexes();
创建单索引
db.collection.ensureIndex({x:1});
创建复合索引
db.collection.ensureIndex({x:1,y:1});
删除索引
db.collection.dropIndex({x:1});
有的集合中存在特殊的符号,例如:CRM3001-201802;
直接输入db.CRM3001-201802.ensureIndex({x:1}); 会报错;
可通过:db.getCollection(“CRM3001-201802”).find({‘username’:’songxinwei’,’kehu_nickname’:’百度25知足常乐’});来设置符合索引;
注意:括号中必须是双引号