$ne¶
-
$ne¶ Syntax:
{field: {$ne: value} }$neselects the documents where the value of thefieldis not equal (i.e.!=) to the specifiedvalue. This includes documents that do not contain thefield.For comparison of different BSON type values, see the specified BSON comparison order.
Consider the following example:
This query will select all documents in the
inventorycollection where theqtyfield value does not equal20, including those documents that do not contain theqtyfield.Consider the following example which uses the
$neoperator with a field in an embedded document:This
update()operation will set theqtyfield value in the documents that contain the embedded documentcarrierwhosestatefield value does not equal “NY”, or where thestatefield or thecarrierembedded document do not exist.The inequality operator
$neis not very selective since it often matches a large portion of the index. As a result, in many cases, a$nequery with an index may perform no better than a$nequery that must scan all documents in a collection. See also Query Selectivity.