How do i get an element from an array in mongodb?

Docs HomeMongoDB Manual

On this page

  • Match an Array
  • Query an Array for an Element
  • Specify Multiple Conditions for Array Elements
  • Additional Query Tutorials


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.


The following example queries for all documents where the field tags value is an array with exactly two elements, "red" and "blank", in the specified order:

If, instead, you wish to find an array that contains both the elements "red" and "blank", without regard to order or other elements in the array, use the $all operator:

The following example queries for all documents where tags is an array that contains the string "red" as one of its elements:

For example, the following operation queries for all documents where the array dim_cm contains at least one element whose value is greater than 25.

When specifying compound conditions on array elements, you can specify the query such that either a single array element meets these condition or any combination of array elements meets the conditions.

The following example queries for documents where the dim_cm array contains elements that in some combination satisfy the query conditions; e.g., one element can satisfy the greater than 15 condition and another element can satisfy the less than 20 condition, or a single element can satisfy both:

Use $elemMatch operator to specify multiple criteria on the elements of an array such that at least one array element satisfies all the specified criteria.

The following example queries for documents where the dim_cm array contains at least one element that is both greater than ($gt) 22 and less than ($lt) 30:

Using dot notation, you can specify query conditions for an element at a particular index or position of the array. The array uses zero-based indexing.

Note

When querying using dot notation, the field and nested field must be inside quotation marks.

The following example queries for all documents where the second element in the array dim_cm is greater than 25:

Use the $size operator to query for arrays by number of elements. For example, the following selects documents where the array tags has 3 elements.

For additional query examples, see:

  • Query Documents

  • Query on Embedded/Nested Documents

  • Query an Array of Embedded Documents

Docs HomeMongoDB Manual

$arrayElemAt

Returns the element at the specified array index.

$arrayElemAt has the following syntax:

{ $arrayElemAt: [ , ] }

The expression can be any valid expression that resolves to an array.

The expression can be any valid expression that resolves to an integer:

  • If the expression resolves to zero or a positive integer, $arrayElemAt returns the element at the idx position, counting from the start of the array.

  • If the expression resolves to a negative integer, $arrayElemAt returns the element at the idx position, counting from the end of the array.

If idx exceeds the array bounds, $arrayElemAt does not return a result.

For more information on expressions, see Expressions.

For more information on expressions, see Expressions.

Example

Results

{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] }

1

{ $arrayElemAt: [ [ 1, 2, 3 ], -2 ] }

2

{ $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

A collection named users contains the following documents:

{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }

The following example returns the first and last element in the favorites array:

db.users.aggregate([
{
$project:
{
name: 1,
first: { $arrayElemAt: [ "$favorites", 0 ] },
last: { $arrayElemAt: [ "$favorites", -1 ] }
}
}
])

The operation returns the following results:

{ "_id" : 1, "name" : "dave123", "first" : "chocolate", "last" : "apples" }
{ "_id" : 2, "name" : "li", "first" : "apples", "last" : "pie" }
{ "_id" : 3, "name" : "ahn", "first" : "pears", "last" : "cherries" }
{ "_id" : 4, "name" : "ty", "first" : "ice cream", "last" : "ice cream" }

  • $first

  • $last

  • $slice

  • Array Expression Operators

How do you get a specific object from an array in MongoDB?

To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object.

How do I query an array element in MongoDB?

To query if the array field contains at least one element with the specified value, use the filter { : } where is the element value. To specify conditions on the elements in the array field, use query operators in the query filter document: { : { : , ... } }

How do I filter an array in MongoDB?

Filter MongoDB Array Element Using $Filter Operator This operator uses three variables: input – This represents the array that we want to extract. cond – This represents the set of conditions that must be met. as – This optional field contains a name for the variable that represent each element of the input array.

How do I find a specific value in MongoDB?

mongodb find field based on variable.
var name = req. params. name;.
var value = req. params. value;.
var query = {};.
query[name] = value;.
collection. findOne(query, function (err, item) { ... });.