* Multifield MongoDB JOINs
MongoDB is a schemaless, document-based database. It’s hard for it to play a role in handling multitable joins that relational databases are remarkably good at. Though later MongoDB versions provide $lookup function to be able to deal with single-field joins, they are barely able to manage multi-field joins. To join collection C1 and collection C2 through C1.sid=C2.sid and C1.mid=C2.mid, for instance:
C1 |
sid |
mid |
sale |
100 |
102 |
8210 |
|
100 |
103 |
4932 |
C2 |
sid |
mid |
buy |
100 |
102 |
4300 |
|
100 |
103 |
5300 |
|
100 |
104 |
7833 |
The expected query result:
sid |
mid |
buy |
sale |
100 |
102 |
8210 |
4300 |
100 |
103 |
4932 |
5300 |
The MongoDB way of doing this is to join the two collection using $lookup function and return a nested structure, split the nested structure into documents using unwind function, filter documents through the combination of redact+cond+$$KEEP+$$PRUNE, and finally display the desired fields. That’s rather complicated.
The process would become simple and convenient if you could use esProc SPL to do the join through C1.sid=C2.sid and C1.mid=C2.mid.
Download esProc installation package HERE.
Directions for accomplishing the task with esProc:
1. Write SPL script cc.dfx in esProc:
A |
B |
|
1 |
=mongo_open("mongodb://127.0.0.1:27017/raqdb") |
/ Connect to MongoDB database |
2 |
=mongo_shell(A1,"C1.find(, {_id: 0})").fetch() |
/ Query data of collection C1 |
3 |
=mongo_shell(A1,"C2.find(, {_id: 0})").fetch() |
/ Query data of collection C2 |
4 |
=A2.join(sid:mid, A3:sid:mid, buy) |
/ Perform join query and append by field to A2 |
5 |
>A1.close() |
/ Close database connection |
2. Execute the script and return the following result:
A4 |
sid |
mid |
buy |
sale |
100 |
102 |
8210 |
4300 |
|
100 |
103 |
4932 |
5300 |
A4: The sid and mid in A2.join() are A2’s fields corresponding to A3’s counterparts.
esProc offers JDBC interface, so you can easily integrate the script into a Java program:
public static void doWork() {
Connection con = null;
java.sql.Statement st;
try{
Class.forName("com.esproc.jdbc.InternalDriver");
con = DriverManager.getConnection("jdbc:esproc:local://");
// Call script cc.dfx
st=con.createStatement();
ResultSet rst = st.executeQuery("call cc");
System.out.println(rst);
} catch(Exception e){
System.out.println(e);
} finally{
// Close database connection
if (con!= null) {
try {
con.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
}
esProc can deal with both multifield join between two collections and a join between multiple collections.
Read How to Call an SPL Script in Java to learn more about integration of esProc SPL script into a Java program.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL