$nin

$nin

Syntax: { field: { $nin: [ <value1>, <value2> ... <valueN> ]} }

$nin selects the documents where:

  • the field value is not in the specified array orfield值不在指定的array中,或者
  • the field does not exist.field不存在。

For comparison of different BSON type values, see the specified BSON comparison order.有关不同BSON类型值的比较,请参阅指定的BSON比较顺序

Consider the following query:考虑下面的查询:

db.inventory.find( { qty: { $nin: [ 5, 15 ] } } )

This query will select all documents in the inventory collection where the qty field value does not equal 5 nor 15. 此查询将选择inventory集合中qty字段值不等于515的所有单据。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:考虑下面的查询:

db.inventory.update( { tags: { $nin: [ "appliances", "school" ] } }, { $set: { sale: false } } )

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

See also参阅

find(), update(), $set.