$ne
定義
$ne
$ne
指定したフィールドの値が指定した値と等しくないドキュメントを選択します。これには、指定したフィールドが含まれていないドキュメントが含まれます。異なる BSON type の値を比較するには、 が指定する 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" ] } } )
出力には、配列が [
"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 } } ]
次のクエリは、 配列内の要素を逆にします。
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 } } ]