On this page本页内容
$eq
¶Specifies equality condition. 指定相等条件。The $eq
operator matches documents where the value of a field equals the specified value.$eq
运算符匹配字段值等于指定值的文档。
Specifying the 指定$eq
operator is equivalent to using the form { field: <value> }
except when the <value>
is a regular expression. $eq
运算符等同于使用{ field: <value> }
形式,除非<value>
是一个正则表达式。See below for examples.请参阅下面的示例。
For comparison of different BSON type values, see the specified BSON comparison order.有关不同BSON类型值的比较,请参阅指定的BSON比较顺序。
If the specified 如果指定了<value>
is a document, the order of the fields in the document matters.<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.有关示例,请参阅等于数组值。
The expression 表达式{ field: <value> }
implicitly specifies a match on <value>
. { field: <value> }
隐式指定了对<value>
的匹配。MongoDB translates the implicit match to a more explicit form.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.有关说明此行为的示例,请参阅正则表达式匹配行为。
Always use the explicit form 始终对用户提供的输入使用显式形式{ field: { $eq: <value> } }
with user-supplied input to avoid problems with maliciously formed queries.{ field: { $eq: <value> } }
,以避免恶意形成的查询出现问题。
The following examples query against the 以下示例使用以下文档查询inventory
collection with the following documents:inventory
集合:
The following example queries the 以下示例查询inventory
collection to select all documents where the value of the qty
field equals 20
:inventory
集合以选择qty
字段值等于20的所有文档:
The query is equivalent to:该查询相当于:
Both queries match the following documents:两个查询都匹配以下文档:
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.要指定嵌入文档中字段的条件,请使用点表示法。
The query is equivalent to:该查询相当于:
Both queries match the following document:两个查询都匹配以下文档:
See also参阅
The following example queries the 以下示例查询inventory
collection to select all documents where the tags
array contains an element with the value "B"
[1]:inventory
集合以选择tags
数组包含值为"B"
[1]的元素的所有文档:
The query is equivalent to:该查询相当于:
Both queries match the following documents:两个查询都匹配以下文档:
See also参阅
[1] | tags field is the string "B" .tags 字段值为字符串"B" 的文档。 |
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
集合以选择tags
数组正好等于指定数组或tags
数组包含等于数组[ "A", "B" ]
的元素的所有文档。
The query is equivalent to:该查询相当于:
Both queries match the following documents:两个查询都匹配以下文档:
The following examples demonstrate the differences in behavior between implicit and explict regular expression matching. 下面的示例演示了隐式和显式正则表达式匹配之间的行为差异。Consider a collection with these documents:考虑这些文件的集合:
A string expands to return the same values whether an implicit match or an explicit use of 无论是隐式匹配还是显式使用$eq
. $eq
,字符串都会展开以返回相同的值。Both of these queries:这两个查询:
return the following result:返回以下结果:
An explicit query using 使用$eq
and a regular expression will only match an object which is also a regular expresssion. $eq
和正则表达式的显式查询将只匹配也是正则表达式的对象。The example query won’t return anything since values in the 示例查询不会返回任何内容,因为company
field are strings.company
字段中的值是字符串。
A query with an implicit match against a regular expression is equivalent to a making a query with the 对正则表达式进行隐式匹配的查询相当于使用$regex
operator. $regex
运算符进行查询。Both of these queries:这两个查询:
return the same results:返回相同的结果: