Database Manual / Reference / Query Language / Query Predicates / Comparison

$eq

$eq
Specifies equality condition. The $eq operator matches documents where the value of a field equals the specified value.指定相等条件。$eq运算符匹配字段值等于指定值的文档。

Compatibility兼容性

You can use $eq for deployments hosted in the following environments:您可以将$eq用于在以下环境中托管的部署:

  • 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 $eq operator has the following form:$eq运算符具有以下形式:

{ <field>: { $eq: <value> } }

Specifying the $eq operator is equivalent to using the form { field: <value> } except when the <value> is a regular expression. See below for examples.指定$eq运算符相当于使用{ field: <value> }形式,除非<value>是正则表达式。示例见下文。

Behavior行为

Comparison Order比较顺序

For comparison of different BSON type values, see the specified BSON comparison order.有关不同BSON类型值的比较,请参阅指定的BSON比较顺序

Match a Document Value匹配文档值

If the specified <value> is a document, the order of the fields in the document matters.如果指定的<value>是文档,则文档中字段的顺序很重要。

Match an Array Value匹配数组值

If the specified <value> is an array, MongoDB matches documents where the <field> matches the array exactly or the <field> contains an element that matches the array exactly. 如果指定的<value>是数组,MongoDB会匹配<field>与数组完全匹配的文档,或者<field>包含与数组完全对应的元素的文档。The order of the elements matters. For an example, see Equals an Array Value.元素的顺序很重要。例如,请参阅等于数组值

Match a Regular Expression匹配正则表达式

The expression { field: <value> } implicitly specifies a match on <value>. MongoDB translates the implicit match to a more explicit form.表达式{ field: <value> }隐式指定了对<value>的匹配。MongoDB将隐式匹配转换为更显式的形式。

When the <value> is fixed, like a particular string, the expression is equivalent to using the $eq operator { field: { $eq: <value> } }.<value>固定时,就像一个特定的字符串一样,表达式等效于使用$eq运算符{ field: { $eq: <value> } }

If <value> is a regular expression, the statement is expanded using the $regex operator { field: { $regex: <value> } }.如果<value>是正则表达式,则使用$regex运算符{ field: { $regex: <value> } }展开语句。

For examples illustrating this behaviour, see Regex Match Behaviour.有关说明此行为的示例,请参阅Regex匹配行为

Security Implications安全影响

Always use the explicit form { field: { $eq: <value> } } with user-supplied input to avoid problems with maliciously formed queries.始终使用用户提供的输入的显式形式{ field: { $eq: <value> } },以避免恶意形成的查询问题。

Examples示例

The following examples query against the inventory collection with the following documents:以下示例使用以下文档查询inventory集合:

db.inventory.insertMany( [
{ _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] },
{ _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] },
{ _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] },
{ _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] },
{ _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
] )

Equals a Specified Value等于指定值

The following example queries the inventory collection to select all documents where the value of the qty field equals 20:以下示例查询inventory集合以选择qty字段值等于20的所有文档:

db.inventory.find( { qty: { $eq: 20 } } )

The query is equivalent to:该查询相当于:

db.inventory.find( { qty: 20 } )

Both queries match the following documents:这两个查询都与以下文档匹配:

[
{ _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] },
{ _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
]

Field in Embedded Document Equals a Value嵌入式文档中的字段等于值

The following example queries the inventory collection to select all documents where the value of the name field in the item document equals "ab". 以下示例查询inventory集合,以选择item文档中name字段值等于"ab"的所有文档。To specify a condition on a field in an embedded document, use the dot notation.要在嵌入式文档中的字段上指定条件,请使用点符号

db.inventory.find( { "item.name": { $eq: "ab" } } )

The query is equivalent to:该查询相当于:

db.inventory.find( { "item.name": "ab" } )

Both queries match the following document:这两个查询都与以下文档匹配:

[ { _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] } ]

Array Element Equals a Value数组元素等于一个值

The following example queries the inventory collection to select all documents where the tags array contains an element with the value "B" [1]:以下示例查询inventory集合以选择标签数组包含值为"B"的元素的所有文档[1]

db.inventory.find( { tags: { $eq: "B" } } )

The query is equivalent to:该查询相当于:

db.inventory.find( { tags: "B" } )

Both queries match the following documents:这两个查询都与以下文档匹配:

[
{ _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] },
{ _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] },
{ _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] },
{ _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] }
]
[1]The query will also match documents where the value of the tags field is the string "B".查询还将匹配tags字段值为字符串"B"的文档。

Equals an Array Value等于数组值

The following example queries the inventory collection to select all documents where the tags array equals exactly the specified array or the tags array contains an element that equals the array [ "A", "B" ].以下示例查询inventory集合,以选择标签数组完全等于指定数组或标签数组包含等于数组[ "A", "B" ]的元素的所有文档。

db.inventory.find( { tags: { $eq: [ "A", "B" ] } } )

The query is equivalent to:该查询相当于:

db.inventory.find( { tags: [ "A", "B" ] } )

Both queries match the following documents:这两个查询都与以下文档匹配:

[
{ _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] },
{ _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
]

Regex Match Behaviour正则表达式匹配行为

The following examples demonstrate the differences in behavior between implicit and explicit regular expression matching. Consider a collection with these documents:以下示例演示了隐式和显式正则表达式匹配之间的行为差异。考虑一个包含以下文档的集合:

db.companies.insertMany( [
{ _id: 001, company: "MongoDB" },
{ _id: 002, company: "MongoDB2" }
] )
$eq match on a string字符串上的匹配

A string expands to return the same values whether an implicit match or an explicit use of $eq. Both of these queries:无论是隐式匹配还是显式使用$eq,字符串都会展开并返回相同的值。这两个问题:

db.collection.find( { company: "MongoDB" }, {_id: 0 })
db.collection.find( { company: { $eq: "MongoDB" } }, {_id: 0 } )

return the following result:返回以下结果:

[ { company: "MongoDB" } ]
$eq match on a regular expression正则表达式上的$eq匹配

An explicit query using $eq and a regular expression will only match an object which is also a regular expresssion. The example query won't return anything since values in the company field are strings.使用$eq和正则表达式的显式查询只会匹配一个也是正则表达式的对象。示例查询不会返回任何内容,因为company字段中的值是字符串。

db.companies.find( { company: { $eq: /MongoDB/ } }, {_id: 0 } )
Regular expression matches正则表达式匹配

A query with an implicit match against a regular expression is equivalent to a making a query with the $regex operator. Both of these queries:对正则表达式进行隐式匹配的查询相当于使用$regex运算符进行查询。这两个问题:

db.companies.find( { company: /MongoDB/ }, {_id: 0 })
db.companies.find( { company: { $regex: /MongoDB/ } }, {_id: 0 } )

return the same results:返回相同的结果:

[
{ company: "MongoDB" },
{ company: "MongoDB2" }
]