Query Selectivity
Some query operations are not selective. These operations cannot useindexes effectively or cannot use indexes at all.
The inequality operators $nin and$ne are notvery selective, as they often match a large portion of the index. As aresult, in most cases, a$nin or$ne query withan index may perform no better than a$nin or$ne query that must scan all documents in a collection.
Queries that specify regular expressions, with inline JavaScriptregular expressions or$regex operator expressions, cannotuse an index with one exception. Queries that specify regularexpressionwith anchors at the beginning of a string can use anindex.
$nin $ne 不会走索引
指定正则表达式和 JavaScript 内联函数 都不会走索引,只有一个例外,查询一个集合的字符串开始出使用正则表达式
在合适的情况下,建立索引可以有效地提升查询效率;但是索引对写效率有影响,所以:
1、找出最频繁的,成为瓶颈的查询语句,在列上建立索引
2、避免创建毫无意义的索引
3、在业务负载低时进行索引创建的操作