Left Outer Join In MongoDB

Posted By : Shashwat Gupta | 17-Apr-2017

 $lookup  in MongoDB  is a operator which performs  "Left Outer Join In MongoDB" , $lookup has four fields that is given below :-

1-  from

2- localField

3-foreignField

 

First field 'form' is use for "where to populate Data" and 'localField' is use for " the field which is available inside the current collection " and 'foreignField' is the field which is use for " the field of that collection where to  populate data " and 'as' is field which is use for "result name(in array)" .one thing that we need to be careful here is with join performance.  "$lookup" function is going to be executed once for each document returned by our $match condition. The $graphLookup operator is a very powerful addition to the MongoDB aggregation framework

 There is - AFAIK - no equivalent of a hash or sort merge join operation possible here, so we need to make sure that we've used an index. he graph lookup can only consume at most 100MB of memory, and currently doesn't spill to disk, even if theallowDiskUse: true clause is specified within the aggregation arguments.   SERVER-23980 is open to correct this but it doesn't appear to have been scheduled 

 Unfortunately, the explain() command doesn’t help us.  It tells us only if we have used an index to perform the initial $match, but doesn't show us if we used an index within the $lookup. 

LocalField and ForeignField must be same according to collection detail. example -

domain.User.aggregate([
{
// $match:{ "referId.id" :{ $exists :true}}
$match:{ 'referId.AddedDate':{$gt:incentiveDate,$lt: afterSevenDate } }
},
{
$group : {
_id : "$referId.id",
addedJeenees : {$sum : 1},
}
} , {
$lookup:
{
from: "users",
localField: "_id",
foreignField: "user_id",
as: "newone"
}
},
{
$project : { _id: 1, addedJeenees : 1 , 'newone.fullName': 1 , 'newone.email':1 ,'newone.userImage':1 }
}
],function (err, total) {});

 

THANKS

About Author

Author Image
Shashwat Gupta

Shashwat is a bright Mean Stack Developer . He has good experience in development of complex UI's of web application and hybrid mobile applications.

Request for Proposal

Name is required

Comment is required

Sending message..