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

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

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

Consider the following query with two $elemMatch clauses: 

db.survey4.find( { "ratings.score": { $elemMatch: { q1: 5, q2: 5 } }, "ratings.certainty": { $elemMatch: { q1: 7, q2: 7 } }, } ) 

Taking the predicates separately: 

• the bounds for the "ratings.score" predicate are the compound bounds: 

{ "ratings.score.q1" : [ [ 5, 5 ] ], "ratings.score.q2" : [ [ 5, 5 ] ] } 

• the bounds for the "ratings.certainty" predicate are the compound bounds: 

{ "ratings.certainty.q1" : [ [ 7, 7 ] ], "ratings.certainty.q2" : [ [ 7, 7 ] ] } 

However, MongoDB cannot compound the bounds for "ratings.score" and "ratings.certainty" since $elemMatch does not join the two. Instead, MongoDB constrains the leading field of the index "ratings.score.q1" which can be compounded with the bounds for "ratings.score.q2" : 

{ "ratings.score.q1" : [ [ 5, 5 ] ], "ratings.score.q2" : [ [ 5, 5 ] ], "ratings.certainty.q1" : [ [ MinKey, MaxKey ] ], "ratings.certainty.q2" : [ [ MinKey, MaxKey ] ] } 

## 8.2.6 Additional Resources 

• Quick Reference Cards 6 

