2

I have two collections listed below :-

table1

{"_id" : ObjectId("5b9a.."), "item_id" :"1.1", "m_date" : "20130401","ref_id":"12R","sub_item_id":"1.1.1"}
{"_id" : ObjectId("5c37.."), "item_id" :"1.1", "m_date" :  "20140401","ref_id":"12R","sub_item_id":"1.1.2"}
{"_id" : ObjectId("123cb.."), "item_id" :"1.2", "m_date" : "20140401","ref_id":"12R","sub_item_id":"1.1.3"}

table2

{"_id" : ObjectId("7cb3.."), "item_id" :"1.1", "m_date" : "20130401","ref_id":"12R","sub_item_id":"1.1.1"}
{"_id" : ObjectId("8f34.."), "item_id" :"1.1", "m_date" : "20140401","ref_id":"13R","sub_item_id":"1.1.2"}
{"_id" : ObjectId("5ec8b.."), "item_id" :"1.2", "m_date" : "20150401","ref_id":"14R","sub_item_id":"1.1.3"}

I would like to display fields from table1 :item_id, m_date, sub_item_id and from table2 : ref_id where item_id:1.1 This must be in both the tables. So expected result should display this :-

{"item_id" :"1.1", "m_date" : "20130401","sub_item_id":"1.1.1","ref_id":"12R"}
{"item_id" :"1.1", "m_date" : "20140401","sub_item_id":"1.1.2","ref_id":"13R"}

I have tried writing below query using $lookup but found 0 Doc

db.table1.aggregate([

{$project:{
    item_id:1,
     m_date: 1, 
     sub_item_id : 1,
    ref_id :1
}},
{
    $lookup: {
        from: 'table2',
        localField: 'item_id',
        foreignField: 'item_id',
        as: 'table2_values'
     },
 },
{$unwind:'$table2_values'},

{ $group: { 
    _id: {ref_id: "$table2_values.ref_id", m_date:  "$m_date"
    ,sub_item_id:'$sub_item_id' },

}},    
{$project:{_id:0,m_date:'$_id.m_date',ref_id:'$_id.ref_id'
,sub_item_id:'$_id.sub_item_id',item_id:1}},
{
 $match: {"table2_values.item_id": "1.1"}
 }

])

Please help me to get the above expected result

Ashh
  • 44,693
  • 14
  • 105
  • 132
SRK
  • 97
  • 2
  • 14

1 Answers1

3

You can try below aggregation with mongodb 3.6

db.table1.aggregate([
  { "$match": { "item_id": "1.1" }},
  { "$lookup": {
    "from": "table2",
    "let": { "item_id": "$item_id", "m_date": "$m_date" },
    "pipeline": [
      { "$match": {
        "$expr": { "$eq": ["$$item_id", "$item_id" ] },
        "$expr": { "$eq": ["$$m_date", "$m_date"] }
      }}
    ],
    "as": "table2_values"
  }},
  { "$addFields": { "ref_id": { "$arrayElemAt": ["$table2_values.ref_id", 0] }}},
  { "$project": { "_id": 0, "item_id": 1, "ref_id": 1 }}
])
Ashh
  • 44,693
  • 14
  • 105
  • 132
  • I want to ftetch fields data from both table, please see the expected output – SRK Nov 06 '18 at 10:28
  • I have checked the output, can you please add the projection to display few specific fields – SRK Nov 06 '18 at 14:07
  • I am able to project specific fields to display using this `{"$project": { "_id":0,"item_id":"$item_id","ref_id":"$ref_id" }}`. Can you please add bit explanation to above query. – SRK Nov 06 '18 at 14:17
  • I have updated my answer and for the explanation you can go through this https://stackoverflow.com/questions/49953780/lookup-multiple-levels-without-unwind – Ashh Nov 07 '18 at 03:39
  • @SRK please accept the answer if it works for you. Else let me know if there is any issue. – Ashh Nov 08 '18 at 11:38