$eq¶
-
$eq
¶ Specifies equality condition. The
$eq
operator matches documents where the value of a field equals the specified 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.
Behavior¶
Comparison Order¶
For comparison of different BSON type values, see the specified BSON comparison order.
Match a Document Value¶
If the specified <value>
is a document, the order of the fields in
the document matters.
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. 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.
When the <value>
is fixed, like a particular string, the expression
is equivalent to using the $eq
operator { field: { $eq: <value> } }
.
If <value>
is a regular expression, the statement is expanded
using the $regex
operator { field: { $regex: <value> } }
.
For examples illustrating this behaviour, see Regex Match Behaviour.
Security Implications¶
Always use the explicit form { field: { $eq: <value> } }
with
user-supplied input to avoid problems with maliciously formed queries.
Examples¶
The following examples query against the inventory
collection with
the following documents:
Equals a Specified Value¶
The following example queries the inventory
collection to select
all documents where the value of the qty
field equals 20
:
The query is equivalent to:
Both queries match the following documents:
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"
. 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
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]:
The query is equivalent to:
Both queries match the following documents:
See also
[1] | The query will also match documents where the
value of the tags field is the string "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" ]
.
The query is equivalent to:
Both queries match the following documents:
Regex Match Behaviour¶
The following examples demonstrate the differences in behavior between implicit and explict regular expression matching. Consider a collection with these documents:
- $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:return the following result:
- $eq match on a regular expression
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 thecompany
field are strings.- 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:return the same results: