Overview of Nested Conditions in Filters
You can use a filter with a nested condition to match a document that has a field with an array value with object elements, where a given object in the array satisfies multiple conditions.
The following nested-condition query matches documents that have both a city value of "Mono Vista" and a state value of "CA" in the same object element of array address.
{ "address[*]" : { "city" : "Mono Vista", "state" : "CA" } }
It specifies that a matching document must have a field address, and if the value of that field is an array then it must have at least one object element that has a city field with value "Mono Vista" and a state field with value "CA".
Of the three sample JSON documents, this filter matches only Example 2-1.
The following filter also matches sample document 1, but it also matches Example 2-2, which has two addresses, one of which has city Mono Vista and the other of which has state CA.
{ "address.city" : "Mono Vista", "address.state" : "CA" }
Unlike the preceding filter, which uses a nested condition clause, nothing here constrains the city and state to belong to the same address. Instead, this filter specifies only that matching documents must have a city field with value "Mono Vista" in some object child of an address field, and a state field with value "CA" in some object of the same address field. It does not specify that fields address.city and address.state must reside within the same object.
That last filter is equivalent to the following one, which has the form of a nested-condition clause but without the [*].
{ "address" : { "city" : "Mono Vista", "state" : "CA" } }
Note: Do not forget to use [*], if your intention is to apply multiple conditions to an object in an array.
Related Topics