It is difficult for MongoDB to realize the operation of cross summarizing. It is also quite complicated to realize it using high-level languages, like Java, after the desired data is retrieved out. In this case, you can consider using esProc to help MongoDB realize the operation. The following example will teach you how it works in detail.
A collection - student - is given in the following:
db.student.insert ( {school:'school1', sname : 'Sean' , sub1: 4, sub2 :5 })
db.student.insert ( {school:'school1', sname : 'chris' , sub1: 4, sub2 :3 })
db.student.insert ( {school:'school1', sname : 'becky' , sub1: 5, sub2 :4 })
db.student.insert ( {school:'school1', sname : 'sam' , sub1: 5, sub2 :4 })
db.student.insert ( {school:'school2', sname : 'dustin' , sub1: 2, sub2 :2 })
db.student.insert ( {school:'school2', sname : 'greg' , sub1: 3, sub2 :4 })
db.student.insert ( {school:'school2', sname : 'peter' , sub1: 5, sub2 :1 })
db.student.insert ( {school:'school2', sname : 'brad' , sub1: 2, sub2 :2 })
db.student.insert ( {school:'school2', sname : 'liz' , sub1: 3, sub2 :null })
We are expected to produce a cross table as the one in the following, in which each row is a school and the first column holds students whose results of sub1 are a 5 and the second column holds those whose results of sub1 are a 4 and so forth.