Nested-Condition Clause (Reference)
You use a filter nested-condition clause to apply multiple conditions at the same time, to array elements that are objects.
A nested-condition clause consists of a parent path, followed by a colon (:), and a single, non-empty nested filter condition.
parent : filter-condition
The path targets a parent object whose value is a child object that satisfies the nested condition. If the parent path ends with [*], then it targets a parent object whose value is either such a child object or an array with such an object as at least one of its elements. The latter case is typical: you end the parent path with [*].
All fields contained in the nested condition are scoped to the parent object. They act as multiple conditions on each of the array objects (or the single child object, if the parent’s value is not an array).
Note: Because the condition of a nested-condition clause follows a field, it cannot contain an ID clause or a special-criterion clause. Those clauses can occur only at the root level.
For example, suppose that field address has an array value with object elements that have fields city and state. The following nested-condition clause tests whether array address has at least one object with both a field address.city that has the value "Boston" and a field address.state that has the value "MA":
"address[*]" : { "city" : "Boston", "state" : "MA" }
Similarly, this nested-condition clause tests whether the value of address.city starts with Bos and address.state has the value "MA":
"address[*]" : { "city" : { "$startsWith : "Bos" }, "state" : "MA" }
Now suppose that you have this document:
{ "address" : [ { "city" : "Boston", "state" : "MA" },
{ "city" : "Los Angeles", "state" : "CA" } ] }
Both of the above nested-condition clauses match that document.
They also match the following document, whose address field value is an object, not an array of objects. The [*] in a nested-condition clause is needed to handle the array case, but it also handles the single-object case.
{ "address" : { "city" : "Boston", "state" : "MA" } }
If you mistakenly omit the [*], then each object element of the array is matched independently against each of the multiple conditions specified.
For example, the following two queries are equivalent. The first one has the form of a nested-condition clause but without the [*]. These queries match each address in a document independently. Each object element of an address array is matched to see if it has a city value of "Boston" or it has a state value of "CA" — it can, but it need not, have both. Each of these queries thus matches the document shown above, which has no single object with both city "Boston" and state "CA".
{ "address" : { "city": "Boston", "state" : "CA" } }
{ "address.city" : "Boston", "address.state" : "CA" }
The following query, with a nested-condition clause for parent field address, does not match the preceding document with an address value that is an array, because that document has no single object in the array with both a field city of value "Boston" and a field state of value "CA".
{ "address[*]" : { "city" : "Boston", "state" : "CA" } }
Related Topics