$elemMatch semantics on arrays of subdocuments
Parent: MongoDB Compound Index Design: ESR, Selectivity, Covering and Multikey Bounds · Published reference · snapshot 2026-10-20 · skill ranked-902d7508
↓ Facts as markdownall context files
Source-anchored research for $elemMatch semantics on arrays of subdocuments. Run status: COMPLETED.
These notes link each claim to its source. Some citations identify archived research reports that are no longer hosted on this site. A published reference means the content is available; it does not certify independent review or accuracy.Read the editorial policy and follow the sources before relying on a claim.
How it works
- A query predicate using $elemMatch on an array of embedded documents selects a parent document only when at least one single array element satisfies every condition inside the operator. A conjunction of dotted-path predicates without $elemMatch can select a parent when different embedded documents satisfy different conditions. (high confidence) [source]
- For a compound multikey index with keys on sibling subfields of the same array, MongoDB can compound the bounds when $elemMatch is applied at the common array path. Placing $elemMatch at a diverging path does not satisfy this path requirement. (medium confidence) [source]
- The query-predicate form of $elemMatch matches a parent document only when the named field is an array with at least one element that satisfies every criterion inside $elemMatch. For an array of subdocuments, all enclosed field predicates must match the same subdocument; a non-array embedded object does not satisfy this array-element condition. (high confidence) [source]
Comparisons and alternatives
- A single $ne predicate has different array semantics with and without $elemMatch: $elemMatch with the field's $ne matches when at least one array element differs from the value, while dotted-path $ne matches only when no array element equals the value. (high confidence) [source]
Facts and statements
- The query-predicate form of $elemMatch filters which parent documents are returned but leaves each matching document's array intact. The separate projection form returns only the first array element that matches its explicit condition. (high confidence) [source]
- For one ordinary positive predicate on a subdocument field, dotted-path syntax can test whether an array element has that value without an explicit $elemMatch. The forms are not universally equivalent: MongoDB's example shows dotted-path syntax also matching a field stored as a non-array embedded object, while $elemMatch matches only arrays. (high confidence) [source]
- Spring Data MongoDB's Criteria.elemMatch(Criteria) and Mongoose's Query.prototype.elemMatch(path, filter) expose builder methods for creating a MongoDB $elemMatch query condition. (high confidence) (verified as of 2026-10-20) [source]
- For multikey indexes, $elemMatch can let MongoDB intersect bounds for predicates on one array field and compound bounds for indexed subfields from the same array. To compound same-array subfield bounds, the indexed paths must share a common path and $elemMatch must target that array path; placing it at a different path does not enable the compounding. (medium confidence) [source]
- Mongoose's query builder exposes the MongoDB $elemMatch predicate through Query.prototype.elemMatch(path, filter) and query.where(path).elemMatch(filter). (low confidence) (verified as of 2026-10-20) [source]
Children
- No children recorded.