MongoDB implementation of cross-summarization is difficult, if the data out, in the Java and other high-level language to summarize, is also quite complex. Therefore, you can consider using the esproc auxiliary MongoDB to complete the cross-summarization. Let's take a look at a concrete example.
The Student collection is as follows:
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})
I hope to be able to count the following crosstab: One school per row, the first column is the number of people with a sub1 score of 5, the second is a sub1 score of 4 , and so on.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5C/23/wKiom1UbVCiBwDJgAAB-3xljjrM561.jpg "style=" float: none; "title=" esproc_mongodb_cross_1.jpg "alt=" Wkiom1ubvcibwdjgaab-3xljjrm561.jpg "/>
The script for the collector is as follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5C/23/wKiom1UbVCnDN0akAAFJfaEK7Ds508.jpg "style=" float: none; "title=" esproc_mongodb_cross_2.jpg "alt=" Wkiom1ubvcndn0akaafjfaek7ds508.jpg "/>
A1: connect MongoDB database,IP and port number is localhost:27017, database is test, username and password are test.
A2: use the Find function to remove the collection student from MongoDBto form a cursor. Esproc uses the same parameter format as the MONGDB find statement in the Find function . Esproc cursors are read and processed in batches to avoid excessive amounts of data and memory overflow situations. There is a small amount of data here, so fetch the function once.
A3: First according to the school group.
A4: After the school is grouped, each group is internally grouped according to the sequence of [1,2,3,4,5] , and then the length of each alignment group is calculated.
A5: The length of the A4 is placed in the corresponding position according to the requirements, resulting in the arrangement of results.
The results are as follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5C/1E/wKioL1UbVWfBQeOjAAB4qBQpwU0640.jpg "style=" float: none; "title=" esproc_mongodb_cross_3.jpg "alt=" Wkiol1ubvwfbqeojaab4qbqpwu0640.jpg "/>
It is necessary to note that the Esproc does not contain MongoDB 's java driver package. to access MongoDB with Esproc, you must advance the Java driver package for MongoDB (requires 2.12.2 or more, such as Mongo-java-driver-2.12.2.jar) is placed in the [esproc installation directory ]\common\jdbc .
The collectorEsprocAssistMongodbthe computed script is easily integrated intoJava, just add a line, writeResultA5you can sendJavaOutputResultSetThe result of the form, the specific code referenceEsprocTutorials. Similarly, withJavacalledEsprocAccessMongodbmust also beMongdbof theJavaThe driver package is placedJavaof the programClasspaththe.
This article is from the High performance report data calculation blog, so be sure to keep this source http://report5.blog.51cto.com/8028595/1630044
Cross-Summary of the collector assistance for MongoDB computing