$ne
¶Syntax: {field: {$ne: value} }
$ne
selects the documents where the value of the 选择field
is not equal to the specified value
. field
值不等于指定value
的文档。This includes documents that do not contain the 这包括不包含该field
.field
的文档。
For comparison of different BSON type values, see the specified BSON comparison order.有关不同BSON类型值的比较,请参阅指定的BSON比较顺序。
Consider the following example:考虑下面的例子:
This query will select all documents in the 此查询将选择inventory
collection where the qty
field value does not equal 20
, including those documents that do not contain the qty
field.inventory
集合中qty
字段值不等于20
的所有单据,包括不包含qty
字段的单据。
Consider the following example which uses the 考虑下面的示例,该示例使用$ne
operator with a field in an embedded document:$ne
运算符与嵌入文档中的字段:
This 此update()
operation will set the qty
field value in the documents that contain the embedded document carrier
whose state
field value does not equal “NY”, or where the state
field or the carrier
embedded document do not exist.update()
操作将在包含state
字段值不等于“NY”的嵌入文档carrier
的文档中,或者在state
字段或carrier
嵌入文档不存在的文档中,设置qty
字段值。
The inequality operator 不等式运算符$ne
is not very selective since it often matches a large portion of the index. $ne
不是很有选择性,因为它通常匹配索引的很大一部分。As a result, in many cases, a 因此,在许多情况下,带有索引的$ne
query with an index may perform no better than a $ne
query that must scan all documents in a collection. $ne
查询的性能可能并不比必须扫描集合中所有文档的$ne查询好。See also Query Selectivity.另请参见查询选择性。