Query Without $elemMatch If the query does not join the conditions on the indexed array fields with $elemMatch , MongoDB cannot compound their bounds. Consider the following query: 

db.survey2.find( { "ratings.score": { $lte: 5 }, "ratings.by": "anon" } ) 

Because a single embedded document in the array does not need to meet both criteria, MongoDB does not compound the bounds. When using a compound index, if MongoDB cannot constrain all the fields of the index, MongoDB always constrains the leading field of the index, in this case "ratings.score" : 

{ "ratings.score": [ [ - Infinity , 5 ] ], "ratings.by": [ [ MinKey, MaxKey ] ] } 

$elemMatch on Incomplete Path If the query does not specify $elemMatch on the path of the embedded fields, up to but excluding the field names, MongoDB cannot compound the bounds of index keys from the same array. 

For example, a collection survey3 contains documents with a field item and an array field ratings : 

{ _id: 1, item: "ABC", ratings: [ { score: { q1: 2, q2: 5 } }, { score: { q1: 8, q2: 4 } } ] } { _id: 2, item: "XYZ", ratings: [ { score: { q1: 7, q2: 8 } }, { score: { q1: 9, q2: 5 } } ] } 

Create a compound index (page 489) on the ratings.score.q1 and the ratings.score.q2 fields: 

db.survey3.createIndex( { "ratings.score.q1": 1, "ratings.score.q2": 1 } ) 

The fields "ratings.score.q1" and "ratings.score.q2" share the field path "ratings.score" and the $elemMatch must be on that path. 

The following query, however, uses an $elemMatch but not on the required path: 

db.survey3.find( { ratings: { $elemMatch: { 'score.q1': 2, 'score.q2': 8 } } } ) 

As such, MongoDB cannot compound the bounds, and the "ratings.score.q2" field will be unconstrained during the index scan. To compound the bounds, the query must use $elemMatch on the path "ratings.score" : 

db.survey3.find( { 'ratings.score': { $elemMatch: { 'q1': 2, 'q2': 8 } } } ) 

Compound $elemMatch Clauses Consider a query that contains multiple $elemMatch clauses on different field paths, for instance, "a.b": { $elemMatch: ... }, "a.c": { $elemMatch: ... } . Mon- goDB cannot combine the bounds of the "a.b" with the bounds of "a.c" since "a.b" and "a.c" also require $elemMatch on the path a . 

For example, a collection survey4 contains documents with a field item and an array field ratings : 

{ _id: 1, item: "ABC", ratings: [ { score: { q1: 2, q2: 5 }, certainty: { q1: 2, q2: 3 } }, 

