Docs Menu

$nin

項目一覧

$nin

$nin selects the documents where:

  • the specified field value is not in the specified array or

  • the specified field does not exist.

次の環境でホストされる配置には $nin を使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

$nin 演算子は次の形式をとります。

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

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 (for example, <value1>, <value2>, and so on).

異なる BSON 型値の比較については、BSON 比較順序の指定を参照してください。

inventory コレクションを次のように作成します。

db.inventory.insertMany( [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] )

The following query selects all documents from the inventory collection where the quantity does ではない equal either 5 or 15.

The query also matches documents that do ではない have a quantity field.

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

出力例:

{ item: 'Pens', quantity: 350, tags: [ 'school', 'office' ] },
{ item: 'Maps', tags: [ 'office', 'storage' ] }

Set the exclude field to true for documents that don't have the "school" tag.

db.inventory.updateMany(
{ tags: { $nin: [ "school" ] } },
{ $set: { exclude: true } }
)

updateMany() also selects a document when the document does not contain the field $nin is matching on.

The inequality operator $nin is ではない very selective since it often matches a large portion of the index. 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. See also クエリの選択性.

以下も参照してください。

項目一覧