Definition定义
$ne$neselects the documents where the value of the specified field is not equal to the specified value. This includes documents that do not contain the specified field.选择指定字段的值不等于指定值的文档。这包括不包含指定字段的文档。For comparison of different BSON type values, see the specified BSON comparison order.有关不同BSON类型值的比较,请参阅指定的BSON比较顺序。
Compatibility兼容性
You can use 您可以将$ne for deployments hosted in the following environments:$ne用于在以下环境中托管的部署:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
The $ne operator has the following form:$ne运算符具有以下形式:
{ field: { $ne: value } }
Note
If the value of the 如果$ne operator is null, see Non-Equality Filter for more information.$ne运算符的值为null,请参阅非相等筛选器以获取更多信息。
Examples示例
The following examples use the 以下示例使用inventory collection. To create the collection run the following insertMany() command in mongosh: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 }
}
] )
Match Document Fields That Are Not Equal匹配不相等的文档字段
Select all documents in the 选择inventory collection where quantity is not equal to 20. This query also selects documents that do not have the quantity field:inventory集合中quantity不等于20的所有单据。此查询还选择没有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 }
}
The SQL equivalent to this query is:与此查询等效的SQL是:
SELECT * FROM INVENTORY WHERE QUANTITIY != 20Update Based on Not Equal Embedded Document Fields基于不相等嵌入式文档字段的更新
The following example sets the 以下示例基于price field based on a $ne comparison against a field in an embedded document. $ne与嵌入式文档中的字段的比较来设置price字段。The updateMany() operation searches for an embedded document, carrier, with a subfield named fee. updateMany()操作搜索一个名为fee的子字段嵌入的文档carrier。It uses 当$set to update the price field to 9.99 in each document where fee has a value that does not equal 1 or where the fee subfield does not exist: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 }
}
The SQL equivalent to this query is:与此查询等效的SQL是:
UPDATE INVENTORY SET PRICE = '9.99' WHERE carrierfee != 1
The inequality operator 不等式运算符$ne is not very selective since it often matches a large portion of the index. $ne不是很有选择性,因为它通常与指数的很大一部分相匹配。As a result, in many cases, a 因此,在许多情况下,带有索引的$ne query with an index may perform no better than a $ne query that must scan all documents in a collection. $ne查询的性能可能不如必须扫描集合中所有文档的$ne搜索。See also Create Selective Queries.另请参见创建选择性查询。
Arrays数组
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:$ne会匹配文档数组与$ne中指定的数组不同的文档。具体来说,$ne匹配以下任何文档中的数组:
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" ]:type数组不同于[ "hardware", "fasteners" ]([“硬件”、“紧固件”])的inventory文档:
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:type数组添加到两个inventory文档中,并使用$ne运行查询:
// Update the nuts document to include a type array更新nuts文档以包含类型数组
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: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 }
}
]
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: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 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: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 }
}
]