If 'main' cube collection has documents with a field that refers to ID of another collection (foreign key) you may configure $lookup stage to resolve fields from this another collection. Depending on relationship cardinality you may configure $lookup stage in one of the two ways:
To illustrate both approaches let's assume that we have 2 collections:
users
with field company_id
companies
with _id
and company_name
fieldsCube uses users as main collection and we want to display company_name
instead of company_id
.
To configure $lookup use Custom Aggregate Pipeline Stages section on the cube configuration form.
{$lookup: {from:"companies", localField:"company_id", foreignField:"_id", as:"Company"} }
{"$unwind": {"path":"$Company", "preserveNullAndEmptyArrays": true } }
Company.company_name
(path to sub-document field after unwind)You can apply custom $lookup + $unwind stages only when report has dimensions that use 'joined' data; for this purpose you can list them in the Apply for Fields.
{$lookup: {from:"companies", localField:"_id.company_id", foreignField:"_id", as:"Company"} }
localField
path should start with _id.
localField
refers to a field in a sub-object replace "." with "__", for example: company.id
→ _id.company__id
{"$unwind": {"path":"$Company", "preserveNullAndEmptyArrays": true } }
Company.company_name
(path to sub-document field after unwind)company_id
(name of the field that is foreign key in the main collection)If you get company_id
values for Company.company_name
dimension ensure that $lookup is configured correctly.
NOTE: both localField
and foreignField
should have the same type.
If foreignField
is objectId()
then localField
values also should be objectId()
;
if localField
is a string then foreignField
should have string type too.