## 5 Application-Layer Inner Join for MongoDB  
As it is well known, the natural join is a clause in the SQL language that combines tuples of two or more relations (tables) in a cartesian product, according to specific criteria applied on attributes. Although not supported, the natural join can be executed by an external third-party application written with whichever programming language supported by the MongoDB Application Program Interface (API), even at the cost of some additional operations. Listing 1 shows an example of inner join query considering \"Exam\" and \"Class\" tables whose instances are depicted respectively in Figures 7 and 8 (only the first 20 rows).  
Figure 9 shows an example of results of the SQL inner join operation performed on MySQL considering \"Exam\" and \"Class\" tables filtered by the \"CourseNumb\" field.  
Figure 8: Example table “Class” on MySQL.  
Figure 7: Example table “Exam” on MySQL.  
Regarding MongoDB, Figures 10 and 11 respectively show analogous examples of \"Exam\" and \"Class\" collections (only the first 20 rows).  
An Inner Join procedure that allows performing an application-layer Inner Join operation considering two MongoDB collections is shown in Algorithm 1. It takes as input three _DBCollection_ objects, respectively representing the two collections that will be joined, _collection1_ and _collection2_, and the resulting collection, i.e., _joinedCollection_. Moreover, Algorithm 1 takes as input also a String, i.e., _jointField_, representing the name of the field for matching documents of collection1 and documents of collection2. In code line 2, DBCursor _cursor1_ is created including all documents of _collection1_. Code lines 3-7 extract documents from _collection1_ in a DBObject[]_array1_. In code line 8, DBCursor _cursor2_ is created including all documents of _collection2_. Code lines 9-13 extract documents from _collection2_ in a DBObject[]_array2_ Code lines 14-29 perform the inner join operation con _collection1_ and _collection2_ whose documents match _jointField_ field. Since, such a procedure performs an Inner Join operation on two MongoDB collections at application-layer, it introduces an overhead due to reading/writing operations in MongoDB.  
Figure 11: Example of Class collection.  
Figure 9: Result of the Inner Join operation performed on MySQL considering “Exam” and “Class” relations filtered by the “CourseNumb” field.  
```
1:procedurevoidjoin(DBCollection collection1,DBCollection collection2,DBCollection collection1,String joinField )
2:DBCursorcurcur1 \(\leftarrow\) collection1.find() ;
3:int sized \(\leftarrow\) cursor1.size();
4:DBObject[] array1 \(\leftarrow\) new DBObject[size1];
5:for\(i=0\)to\(i<size1\)do
6:array[][] \(\leftarrow\) cursor1.next();
7:endfor
8:DBCursorcur2 \(\leftarrow\) collection2.find();
9:intsize2 \(\leftarrow\) cursor2.size();
10:DBObject[] array2 \(\leftarrow\) new DBObject[size2];
11:for\(i=0\)to\(i<size2\)do
12:array[] \(\leftarrow\) cursor2.next();
13:endfor
14:DBObject obj1 \(\leftarrow\) (DBObject) JSON.parse(')';
15:for\(i=0\)to\(i<size1\)do
16:ifarray[].containsField(joinField)then
17:obj1.putAll(array1[]);
18:for\(j=0\)to\(i<size2\)do
19:ifarray2[].containsField(joinField)then
20:ifarray[][].containsField(joinField)then
21:ifarray[][].containsField(joinField)then
22:ifarray[][].containsField(joinField).equals(array2[]).get(joinField)) then
23:obj1.putAll(array2[]) ;
24:obj1.removeField('_id');
25:collectionJoined.insert(obj1);
26:obj1 \(\leftarrow\) JSON.parse(')';
27:endif
28:endif
29:endfor
30:endif
31:endfor
32:endfor
33:endprocedure
```  
**Algorithm 1** Procedure performing an Inner Join operation among two collections in MongoDB.  
The MongoDB Inner Join procedure allows executing a query analogous to the one expressed in Listing 1. The result of the MongoDB Inner Join operation considering \"Exam\" and \"Class\" collections filtered by the \"CourseNumb\" field is shown in Figure 12.