$nin
¶Syntax: { field: { $nin: [ <value1>, <value2> ... <valueN> ]} }
$nin
selects the documents where:
field
value is not in the specified array
orfield
值不在指定的array
中,或者field
does not exist.field
不存在。For comparison of different BSON type values, see the specified BSON comparison order.有关不同BSON类型值的比较,请参阅指定的BSON比较顺序。
Consider the following query:考虑下面的查询:
This query will select all documents in the 此查询将选择inventory
collection where the qty
field value does not equal 5
nor 15
. inventory
集合中qty
字段值不等于5
或15
的所有单据。The selected documents will include those documents that do not contain the 所选文档将包括不包含qty
field.qty
字段的文档。
If the 如果field
holds an array, then the $nin
operator selects the documents whose field
holds an array with no element equal to a value in the specified array (e.g. <value1>
, <value2>
, etc.).field
包含数组,则$nin
运算符将选择其field
包含的数组中的元素不等于指定数组中的值(例如<value1>
、<value2>
)的文档。
Consider the following query:考虑下面的查询:
This 此update()
operation will set the sale
field value in the inventory
collection where the tags
field holds an array with no elements matching an element in the array ["appliances", "school"]
or where a document does not contain the tags
field.update()
操作将在inventory
集合中设置sale
字段值,其中tags
字段保存的数组中没有与数组["appliances", "school"]
中的元素匹配的元素,或者文档不包含tags
字段。
The inequality operator 不等式运算符$nin
is not very selective since it often matches a large portion of the index. $nin
不是很有选择性,因为它通常匹配索引的很大一部分。As a result, in many cases, a 因此,在许多情况下,带有索引的$nin
query with an index may perform no better than a $nin
query that must scan all documents in a collection. $nin
查询的性能可能并不比必须扫描集合中所有文档的$nin
查询好。See also Query Selectivity.另请参见查询选择性。