$eq

  • $eq

New in version 3.0.

Specifies equality condition. The $eq operator matchesdocuments where the value of a field equals the specified value.

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

The $eq expression is equivalent to { field: <value> }.

Behavior

Comparison Order

For comparison of different BSON type values, see the specifiedBSON comparison order.

Match a Document Value

If the specified <value> is a document, the order of the fields inthe document matters.

Match an Array Value

If the specified <value> is an array, MongoDB matches documentswhere the <field> matches the array exactly or the <field>contains an element that matches the array exactly. The order of theelements matters. For an example, see Equals an Array Value.

Examples

The following examples query against the inventory collection withthe following documents:

  1. { _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] }
  2. { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] }
  3. { _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] }
  4. { _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] }
  5. { _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 selectall documents where the value of the qty field equals 20:

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

The query is equivalent to:

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

Both queries match the following documents:

  1. { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] }
  2. { _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 selectall documents where the value of the name field in the itemdocument equals "ab". To specify a condition on a field in anembedded document, use the dot notation.

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

The query is equivalent to:

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

Both queries match the following document:

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

See also

Query Embedded Documents

Array Element Equals a Value

The following example queries the inventory collection to selectall documents where the tags array contains an element with thevalue "B" [1]:

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

The query is equivalent to:

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

Both queries match the following documents:

  1. { _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] }
  2. { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] }
  3. { _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] }
  4. { _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] }

See also

$elemMatch, Query Arrays

[1]The query will also match documents where thevalue of the tags field is the string "B".

Equals an Array Value

The following example queries the inventory collection to selectall documents where the tags array equals exactly the specifiedarray or the tags array contains an element that equals the array[ "A", "B" ].

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

The query is equivalent to:

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

Both queries match the following documents:

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