Perform join using MongoDB lookup Aggregate

Perform a join between collections using MongoDB:

Get articles with users:

Create collections users and articles with the following documents:

use YAMICODE
db.users.insert([
	{name: "yami", email: "yamicodeofficial@gmail.com"},
	{name: "code", email: "yamicodeofficial2@gmail.com"}
])
db.articles.insert([
	{article: "yami code first article", user: "yamicodeofficial@gmail.com"}, 
	{article: "yami code second article", user: "yamicodeofficial2@gmail.com"}, 
	{article: "yami code third article", user: "yamicodeofficial@gmail.com"}, 
	{article: "yami code forth article", user: null}, 
	{article: "yami code fifth article"}
])

The following aggregation operation on articles collection join the documents from collection articles on the field user with the users collection on the field email and return an array containing the users.

db.articles.aggregate([
	{
		$lookup:
		{
			from: "users",
			localField: "user",
			foreignField: "email",
			as: "user"
		},
	}
])

The operation returns the following document:

{ 
"_id" : ObjectId("5c460b4ac5788ef22626569f"), 
"article" : "yami code first article", 
"user" : [ 
		{ "_id" : ObjectId("5c460b0fc5788ef22626569d"), "name" : "yami", "email" : "yamicodeofficial@gmail.com" } 
	] 
}
{ 
"_id" : ObjectId("5c460b4ac5788ef2262656a0"), 
"article" : "yami code second article", 
"user" : [ 
		{ "_id" : ObjectId("5c460b0fc5788ef22626569e"), "name" : "code", "email" : "yamicodeofficial2@gmail.com" } 
	] 
}
{ 
"_id" : ObjectId("5c460b4ac5788ef2262656a1"), 
"article" : "yami code third article", 
"user" : [ 
	{ "_id" : ObjectId("5c460b0fc5788ef22626569d"), "name" : "yami", "email" : "yamicodeofficial@gmail.com" } 
	] 
}
{ 
"_id" : ObjectId("5c460b4ac5788ef2262656a2"), 
"article" : "yami code forth article", 
"user" : [ ] }
{ 
"_id" : ObjectId("5c460b4ac5788ef2262656a3"), 
"article" : "yami code fifth article", 
"user" : [ ] 
}

The following aggregation pipelines perform a join and ignore the empty results.

db.articles.aggregate([
	{
		$lookup:
		{
			from: "users",
			localField: "user",
			foreignField: "email",
			as: "user"
		},
	},
	{
		$match: 
		{ 
			"user": 
			{
				$ne: [] 
			} 
		}
	}
])

The operation returns the following document:

{ 
	"_id" : ObjectId("5c460b4ac5788ef22626569f"), 
	"article" : "yami code first article", 
	"user" : [ { 
		"_id" : ObjectId("5c460b0fc5788ef22626569d"), 
		"name" : "yami", 
		"email" : "yamicodeofficial@gmail.com" 
	} 
	]	 
}
{ 
	"_id" : ObjectId("5c460b4ac5788ef2262656a0"), 
	"article" : "yami code second article", 
	"user" : [ { 
		"_id" : ObjectId("5c460b0fc5788ef22626569e"), 
		"name" : "code", 
		"email" : "yamicodeofficial2@gmail.com" } 
	] 
}
{ 
	"_id" : ObjectId("5c460b4ac5788ef2262656a1"), 
	"article" : "yami code third article", 
	"user" : [ { 
		"_id" : ObjectId("5c460b0fc5788ef22626569d"), 
		"name" : "yami", 
		"email" : "yamicodeofficial@gmail.com" } 
	]
}

In order to get the user as object instead of array you’ll need to $unwind the array as one stage of the aggregation pipeline.

db.articles.aggregate([
	{
		$lookup:
		{
			from: "users",
			localField: "user",
			foreignField: "email",
			as: "user"
		},
	},
	{
		$match: 
		{ 
			"user": 
			{
				$ne: [] 
			} 
		}
	},
	{
      $unwind: "$user"
   }
])

The operation returns the following document:

{ 
	"_id" : ObjectId("5c460b4ac5788ef22626569f"), 
	"article" : "yami code first article", 
	"user" : { 
		"_id" : ObjectId("5c460b0fc5788ef22626569d"), 
		"name" : "yami", 
		"email" : "yamicodeofficial@gmail.com" } 
}
{ 
	"_id" : ObjectId("5c460b4ac5788ef2262656a0"), 
	"article" : "yami code second article", 
	"user" : { 
		"_id" : ObjectId("5c460b0fc5788ef22626569e"), 
		"name" : "code", 
		"email" : "yamicodeofficial2@gmail.com" } 
}
{ 
	"_id" : ObjectId("5c460b4ac5788ef2262656a1"), 
	"article" : "yami code third article", 
	"user" : {
		"_id" : ObjectId("5c460b0fc5788ef22626569d"), 
		"name" : "yami", 
		"email" : "yamicodeofficial@gmail.com" } 
}

Get users with articles:

The following aggregation operation on users collection join the documents from collection users on the field email with the articles collection on the field user and return an array containing the articles.

db.users.aggregate([
	{
		$lookup:
		{
			from: "articles",
			localField: "email",
			foreignField: "user",
			as: "articles"
		},
	}
])

The operation returns the following document:

{ 
	"_id" : ObjectId("5c460b0fc5788ef22626569d"), 
	"name" : "yami", 
	"email" : "yamicodeofficial@gmail.com", 
	"articles" : [ 
		{ 
			"_id" : ObjectId("5c460b4ac5788ef22626569f"), 
			"article" : "yami code first article", 
			"user" : "yamicodeofficial@gmail.com" }
		{ 
			"_id" : ObjectId("5c460b4ac5788ef2262656a1"), 
			"article" : "yami code third article", 
			"user" : "yamicodeofficial@gmail.com" 
		} 
	] 
}
{ 
	"_id" : ObjectId("5c460b0fc5788ef22626569e"), 
	"name" : "code", 
	"email" : "yamicodeofficial2@gmail.com", 
	"articles" : [ 
		{ 
			"_id" : ObjectId("5c460b4ac5788ef2262656a0"), 
			"article" : "yami code second article", 
			"user" : "yamicodeofficial2@gmail.com" 
		} 
	] 
}