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