$ne

$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:考虑下面的例子:

db.inventory.find( { qty: { $ne: 20 } } )

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运算符与嵌入文档中的字段:

db.inventory.update( { "carrier.state": { $ne: "NY" } }, { $set: { qty: 20 } } )

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.另请参见查询选择性

See also参阅

find(), update(), $set.