How to Do Joins in MongoDB

Key words: MongoDB Joins

MongoDB is a document-based NoSQL database that supports distributed storage. Its BSON format represents attributes better than relational databases do. But the NoSQL database isn’t that good at structured processing. It doesn’t give a strong enough support for operations such as joins, group and subqueries. For complicated computations, users have to read MongoDB data out to process. Here the problem is that it’s difficult to program those computations in Java.

Though MongoDB has $lookup to do simple join operations, programming multi-table joins is still not that easy, even complicated, in real-world businesses. To JOIN two sets, for example, we have part of the source data below:



user1  user2    income

1  2  0.56

1  3  0.26

user1  user2    output

1  2  0.3

1  3  0.4

2  3  0.5

Expected result:

user1  user2  income    output

1  2  0.56    0.3

1  3  0.26    0.4

A Mongo script does it in this way:


{    "$lookup": {

"from":   "c2",



"as":   "collection2_doc"


 {  "$unwind":"$collection2_doc"},

{    "$redact": {

  "$cond": [

{"$eq":   [ "$user2",   "$collection2_doc.user2"] },





{    "$project": {

"user1":   1,

"user2":   1,

"income":   "$income",

"output":   "$collection2_doc. output"



The join would be rather simple if we could use eProc to do it. esProc SPL boasts a complete set of set-operation function library and agile syntax to handle it in a simple way. It helps make MongoDB great in structured processing. Here’s the 4-line SPL script for joining the two sets:








With the help of esProc SPL, we can conveniently handle structured data processing in MongoDB. For more examples, read esProc-driven MongoDB Hackers, Simplifying MongoDB Data Association, and How SPL Assists MongoDB Calculation.

esProc is integration-friendly. Read How to Call an SPL Script in Java to see how we can easily embedded an SPL script into a Java program.

Read Getting Started with esProc to download and install esProc, get a license for free and find related documentation.