$ne
定義
$ne
$ne
は、指定したフィールドの値が指定した値と等しくないドキュメントを選択します。 これには、指定したフィールドが含まれていないドキュメントが含まれます。異なる BSON 型値の比較については、BSON 比較順序の指定を参照してください。
互換性
次の環境でホストされる配置には $ne
を使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
$ne
演算子は次のような形をとります。
{ field: { $ne: value } }
注意
$ne
演算子の値が null の場合、詳細については「非等価フィルター」を参照してください。
例
次の例では、inventory
コレクションを使用しています。コレクションを作成するには、mongosh
で
insertMany()
コマンドを実行します。
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 } } ] )
等しくないドキュメント フィールドのマッチング
quantity
が 20
と等しくない 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
クエリよりもパフォーマンスが良くありません。「クエリの選択性」も参照してください。
配列
配列を比較する際、$ne
はドキュメント配列が $ne
で指定された配列と異なるドキュメントを一致させます。具体的に言うと、$ne
は次の配列のいずれかに一致するドキュメントに一致します。
異なる要素の値または文字列を持つ
要素が異なる順序で配置されている
要素の数が異なります
ドキュメントから欠落しています
例えば、次のクエリは、type
配列が [ "hardware", "fasteners" ]
と異なる inventory
ドキュメントを返します。
db.inventory.find( { type: { $ne: [ "hardware", "fasteners" ] } } )
次の完全な例では、type
配列を2つの inventory
ドキュメントに追加し、$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" ] } } )
出力には nuts
ドキュメントが表示されます。これは、配列が [
"hardware", "fasteners" ]
と異なるためであり、washers
ドキュメントは type
配列がないためです。
[ { _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 } } ]
次のクエリは、配列内の要素を逆順にします。
db.inventory.find( { type: { $ne: [ "fasteners", "hardware" ] } } )
bolts
ドキュメントの type
配列が [ "hardware",
"fasteners" ]
であり、[ "fasteners", "hardware" ]
とは異なるため、クエリは nuts
と washers
ドキュメントに加えて bolts
ドキュメントを返します。
[ { _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
は、配列に指定された値が含まれていないドキュメントおよび配列を持たないドキュメントに一致します。以下に例を挙げます。
db.inventory.find( { type: { $ne: "fasteners" } } )
配列[ "hardware"
]
が"fasteners"
と異なるため、クエリはnuts
のドキュメントを返します。また、ドキュメントにtype
配列がないため、クエリはwashers
ドキュメントを返します。クエリ出力:
[ { _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 } } ]