The latest version of MongoDB (v3.2) adds support for partial (filtered) indexes. You supply a filter when you create the index and that filter determines which documents will be referenced in the index and which will not.
Can I use any filter expression (as long as it's a valid filter)? Or are there limitations to the filter being used? If so, what are those limitations?
No, partial indexes support only a subset of the operators in the filter used. The only supported operators are:
$AND
(only at the top level),$EQ
,$LT
,$LTE
,$GT
,$GTE
,$EXISTS
and theTYPE_OPERATOR
.That leaves out, for example
$NOT
,$REGEX
,$OR
, etc.You can see that in the source for MongoDB here.
There are also some general limitations on partial indexes:
_id
indexes can't be partial indexes.