Docs Menu

$ne

$ne

$neは、指定したフィールドの値が指定した値と等しくないドキュメントを選択します。 これには、指定したフィールドが含まれていないドキュメントが含まれます。

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

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

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

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

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

$ne演算子は次のような形をとります。

{ field: { $ne: value } }

注意

$ne 演算子の値が null の場合、詳細については「非等価フィルター」を参照してください。

次の例では、inventoryコレクションを使用しています。コレクションを作成するには、mongoshinsertMany()コマンドを実行します。

db.inventory.insertMany( [
{
"item": "nuts", "quantity": 30,
"carrier": { "name": "Shipit", "fee": 3 }
},
{
"item": "bolts", "quantity": 50,
"carrier": { "name": "Shipit", "fee": 4 }
},
{
"item": "washers", "quantity": 10,
"carrier": { "name": "Shipit", "fee": 1 }
}
] )

quantity20 と等しくない inventory コレクション内のすべてのドキュメントを選択します。このクエリは、quantity フィールドがないドキュメントも選択します。

db.inventory.find( { quantity: { $ne: 20 } } )
{
_id: ObjectId("61ba667dfe687fce2f042420"),
item: 'nuts',
quantity: 30,
carrier: { name: 'Shipit', fee: 3 }
},
{
_id: ObjectId("61ba667dfe687fce2f042421"),
item: 'bolts',
quantity: 50,
carrier: { name: 'Shipit', fee: 4 }
},
{
_id: ObjectId("61ba667dfe687fce2f042422"),
item: 'washers',
quantity: 10,
carrier: { name: 'Shipit', fee: 1 }
}

このクエリに相当する SQL は次のとおりです。

SELECT * FROM INVENTORY WHERE QUANTITIY != 20

次の例では、埋め込みドキュメント内のフィールドと $ne を比較して price フィールドを設定します。updateMany() 操作では、サブフィールド名が fee と設定されている埋め込みドキュメント carrier を検索します。fee の値が 1 と等しくない場合、または fee サブフィールドが存在しない場合に、各ドキュメントで $set を使用して price フィールドを 9.99 に更新します。

db.inventory.updateMany(
{ "carrier.fee" : { $ne: 1 } },
{ $set: { "price": 9.99 } }
)
{
_id: ObjectId("61ba66e2fe687fce2f042423"),
item: 'nuts',
quantity: 30,
carrier: { name: 'Shipit', fee: 3 },
price: 9.99
},
{
_id: ObjectId("61ba66e2fe687fce2f042424"),
item: 'bolts',
quantity: 50,
carrier: { name: 'Shipit', fee: 4 },
price: 9.99
},
{
_id: ObjectId("61ba66e2fe687fce2f042425"),
item: 'washers',
quantity: 10,
carrier: { name: 'Shipit', fee: 1 }
}

このクエリに相当する SQL は次のとおりです。

UPDATE INVENTORY SET PRICE = '9.99' WHERE carrierfee != 1

不等価演算子 $ne はインデックスの大部分と一致することが多いため、あまり選択的ではありません。その結果、多くの場合、インデックス付きの $ne クエリは、コレクション内のすべてのドキュメントをスキャンする必要がある $ne クエリよりもパフォーマンスが良くありません。「クエリの選択性」も参照してください。

When comparing arrays, $ne matches documents where the document array differs from the array specified in $ne. Specifically, $ne matches any of these documents where the arrays:

  • have different element values or strings

  • have elements in a different order

  • have a different number of elements

  • are missing from a document

For example, the following query returns inventory documents where the type array differs from [ "hardware", "fasteners" ]:

db.inventory.find( { type: { $ne: [ "hardware", "fasteners" ] } } )

The following complete example adds a type array to two inventory documents and runs a query with $ne:

// Update the nuts document to include a type array
db.inventory.updateOne(
{ item: "nuts" },
{ $set: { type: [ "hardware" ] } }
)
// Update the bolts document to include a type array
db.inventory.updateOne(
{ item: "bolts" },
{ $set: { type: [ "hardware", "fasteners" ] } }
)
// Find documents where the type array differs from [ "hardware", "fasteners" ]
db.inventory.find( { type: { $ne: [ "hardware", "fasteners" ] } } )

Output shows the nuts document because the array differs from [ "hardware", "fasteners" ], and the washers document because it doesn't have a type array:

[
{
_id: ObjectId('679d26907c5a58595234305c'),
item: 'nuts',
quantity: 30,
carrier: { name: 'Shipit', fee: 3 },
type: [ 'hardware' ]
},
{
_id: ObjectId('679d26907c5a58595234305e'),
item: 'washers',
quantity: 10,
carrier: { name: 'Shipit', fee: 1 }
}
]

The following query reverses the elements in the array:

db.inventory.find( { type: { $ne: [ "fasteners", "hardware" ] } } )

Because the type array in the bolts document is [ "hardware", "fasteners" ], which differs from [ "fasteners", "hardware" ], the query returns the bolts document in addition to the nuts and washers documents:

[
{
_id: ObjectId('679d26907c5a58595234305c'),
item: 'nuts',
quantity: 30,
carrier: { name: 'Shipit', fee: 3 },
type: [ 'hardware' ]
},
{
_id: ObjectId('679d26907c5a58595234305d'),
item: 'bolts',
quantity: 50,
carrier: { name: 'Shipit', fee: 4 },
type: [ 'hardware', 'fasteners' ]
},
{
_id: ObjectId('679d26907c5a58595234305e'),
item: 'washers',
quantity: 10,
carrier: { name: 'Shipit', fee: 1 }
}
]

$ne matches documents where the array doesn't contain the specified value and documents that don't have the array. For example:

db.inventory.find( { type: { $ne: "fasteners" } } )

The query returns the nuts document because the array [ "hardware" ] differs from "fasteners". Also, the query returns the washers document because the document doesn't have a type array. Query output:

[
{
_id: ObjectId('679d26907c5a58595234305c'),
item: 'nuts',
quantity: 30,
carrier: { name: 'Shipit', fee: 3 },
type: [ 'hardware' ]
},
{
_id: ObjectId('679d26907c5a58595234305e'),
item: 'washers',
quantity: 10,
carrier: { name: 'Shipit', fee: 1 }
}
]