How do i get the elements of 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 I retrieve an array in MongoDB?

MongoDB query array operator is used to query documents with an array, we can retrieve array element of data by using query array operator in MongoDB. There are three types of query array operators available in MongoDB, we need to use prefix as $ sign before using query array operator.

How do I pull all elements in an array in MongoDB?

The $pullAll operator removes all instances of the specified values from an existing array. Unlike the $pull operator that removes elements by specifying a query, $pullAll removes elements that match the listed values.

How do I select 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 see elements in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.