How do i query an array field 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

On this page

  • Query for a Document Nested in an Array
  • Specify a Query Condition on a Field in an Array of Documents
  • Specify Multiple Conditions for Array of Documents
  • 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 selects all documents where an element in the instock array matches the specified document:

Equality matches on the whole embedded/nested document require an exact match of the specified document, including the field order. For example, the following query does not match any documents in the inventory collection:

If you do not know the index position of the document nested in the array, concatenate the name of the array field, with a dot [.] and the name of the field in the nested document.

The following example selects all documents where the instock array has at least one embedded document that contains the field qty whose value is less than or equal to 20:

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

Note

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

The following example selects all documents where the instock array has as its first element a document that contains the field qty whose value is less than or equal to 20:

When specifying conditions on more than one field nested in an array of documents, you can specify the query such that either a single document meets these condition or any combination of documents [including a single document] in the array meets the conditions.

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

The following example queries for documents where the instock array has at least one embedded document that contains both the field qty equal to 5 and the field warehouse equal to A:

The following example queries for documents where the instock array has at least one embedded document that contains the field qty that is greater than 10 and less than or equal to 20:

If the compound query conditions on an array field do not use the $elemMatch operator, the query selects those documents whose array contains any combination of elements that satisfies the conditions.

For example, the following query matches documents where any document nested in the instock array has the qty field greater than 10 and any document [but not necessarily the same embedded document] in the array has the qty field less than or equal to 20:

The following example queries for documents where the instock array has at least one embedded document that contains the field qty equal to 5 and at least one embedded document [but not necessarily the same embedded document] that contains the field warehouse equal to A:

For additional query examples, see:

  • Query an Array

  • Query Documents

  • Query on Embedded/Nested Documents

How do I query a field in an array 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 pull an array in MongoDB?

The $pull operator removes from an existing array all instances of a value or values that match a specified condition. The $pull operator has the form: { $pull: { : , : , ... } } To specify a in an embedded document or in an array, use dot notation.

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 search for a specific field in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find[{"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0}];

Chủ Đề