i want to have a lookup between two nested object arrays in MongoDB

Viewed 28

I have the following two collections 1)template_data 2)static_data

template_data

{
    "_id" : "e1a0a44a-abc1-4185-aefc-2194597a62a2",
    "templateName" : "IB Tradebook",
    "period" : "Jul-2020",
    "attributeData" : [ 
        {
            "attributeName" : "BUY/SELL",
            "attributeValue" : "B"
        }, 
        {
            "attributeName" : "EXCHANGE ID",
            "attributeValue" : "NYBT"
        }, 
        {
            "attributeName" : "ACCOUNT NUMBER",
            "attributeValue" : "XXXXXXX"
        }, 
        {
            "attributeName" : "BTML NAME",
            "attributeValue" : "CCCCCCCCCCCCCCCCCCCCCC"
        }, 
        {
            "attributeName" : "TRADE SUMMARY ID",
            "attributeValue" : "AAA-BBBB-CCCC"
        }, 
        {
            "attributeName" : "TRADER NAME",
            "attributeValue" : "TOM"
        }]
},
{
    "_id" : "e1a0a44a-abc1-4185-aefc-2194597a62a2",
    "templateName" : "CB Tradebook",
    "period" : "Jul-2020",
    "attributeData" : [ 
        {
            "attributeName" : "BUY/SELL",
            "attributeValue" : "B"
        }, 
        {
            "attributeName" : "EXCHANGE ID",
            "attributeValue" : "NYBT"
        }, 
        {
            "attributeName" : "ACCOUNT NUMBER",
            "attributeValue" : "XXXXXXX"
        }, 
        {
            "attributeName" : "BTML NAME",
            "attributeValue" : "CCCCCCCCCCCCCCCCCCCCCC"
        }, 
        {
            "attributeName" : "TRADE SUMMARY ID",
            "attributeValue" : "AAA-BBBB-CCCC"
        }, 
        {
            "attributeName" : "TRADER NAME",
            "attributeValue" : "TOM"
        }]
}

static_data

{
    "_id" : "ce62f96f-5ef5-4bb0-8bab-9b0a743f968c",
    "templateName" : "User_Inventory",
    "period" : "Jul-2020",
    "attributeData" : [ 
        {
            "attributeName" : "TRADING SYSTEM",
            "attributeValue" : "ZZZZ",
        }, 
        {
            "attributeName" : "CLIENT NAME",
            "attributeValue" : "XXX TRADING COMPANY",
        }, 
        {
            "attributeName" : "EFLOW USER ID",
            "attributeValue" : "",
        }, 
        {
            "attributeName" : "TRADER EMAIL ID",
            "attributeValue" : "xxxx@goole.com",
        }, 
        {
            "attributeName" : "USER ID",
            "attributeValue" : "TOM"
        }]
},
{
    "_id" : "ce62f96f-5ef5-4bb0-8bab-9b0a743f968c",
    "templateName" : "Client_Inventory",
    "period" : "Jul-2020",
    "attributeData" : [ 
        {
            "attributeName" : "TRADING SYSTEM",
            "attributeValue" : "ZZZZ",
        }, 
        {
            "attributeName" : "CLIENT NAME",
            "attributeValue" : "XXX TRADING COMPANY",
        }, 
        {
            "attributeName" : "EFLOW USER ID",
            "attributeValue" : "",
        }, 
        {
            "attributeName" : "TRADER EMAIL ID",
            "attributeValue" : "xxxx@goole.com",
        }, 
        {
            "attributeName" : "USER ID",
            "attributeValue" : "TOM"
        }]
}

My requirement is 1.From template_data collection i have filter all the documents with templateName = 'IB Tradebook', period = 'Jul-2020'

2.From statci_data collection i have filter all the documents with templateName = 'User_Inventory', period = 'Jul-2020'

3.filter condition from both collections

template_data.attributeData.attributeName = "TRADER NAME" static_data.attributeData.attributeName = "USER ID"

SQL Equivalent:

Select template_data.attributeData.attributeName, template_data.attributeData.attributeValue,
       static_data.attributeData.attributeName, static_data.attributeData.attributeValue
  from template_data
  left join static_data on (template_data.attributeData.attributeValue = static_data.attributeData.attributeValue)
 Where template_data.attributeData.attributeName in ("BUY/SELL", "EXCHANGE ID", "TRADER NAME")
   and static_data.attributeData.attributeName in ("TRADING SYSTEM","CLIENT NAME", "USER ID")
   and template_data.templateName = 'IB Tradebook' and template_data.period = 'Jul-2020'
   and static_data.templateName = 'User_Inventory' and static_data.period = 'Jul-2020';

db.template_data.aggregate([
    {
        $lookup :
         { from : "static_data",
           localField: "template_data.attributeData.atributeValue"
           ForeignField : "static_data.attributeData.atributeValue"
           as : "static_details"
         }
    }

4.$project and expected results

for the above condition the "template_data.attributeData.atributeValue" (TOM) matches with "static_data.attributeData.atributeValue" (TOM)

AttributNames to be considered in output

template_data : attributeName["BUY/SELL", "EXCHANGE ID", "TRADER NAME"] static_data : attributeName["TRADING SYSTEM","CLIENT NAME", "USER ID" ]

output :

{
    "_id" : "e1a0a44a-abc1-4185-aefc-2194597a62a2",
    "templateName" : "IB Tradebook",
    "period" : "Jul-2020",
    "attributeData" : [ 
        {
            "attributeName" : "BUY/SELL", 
            "attributeValue" : "B"
        }, 
        {
            "attributeName" : "EXCHANGE ID",
            "attributeValue" : "NYBT"
        },
        {
            "attributeName" : "TRADER NAME",
            "attributeValue" : "TOM"
        }
],
 static_details : {
    "_id" : "ce62f96f-5ef5-4bb0-8bab-9b0a743f968c",
    "templateName" : "User_Inventory",
    "period" : "Jul-2020",
    "attributeData" : [ 
        {
            "attributeName" : "TRADING SYSTEM",
            "attributeValue" : "ZZZZ",
        }, 
        {
            "attributeName" : "CLIENT NAME",
            "attributeValue" : "XXX TRADING COMPANY",
        }, 
        {
            "attributeName" : "USER ID",
            "attributeValue" : "TOM"
        }]
    }
}
0 Answers
Related