Skip to content

Instantly share code, notes, and snippets.

@yogendra
Created January 30, 2018 13:36
Show Gist options
  • Save yogendra/74b3eab570ad1be755212d887500f836 to your computer and use it in GitHub Desktop.
Save yogendra/74b3eab570ad1be755212d887500f836 to your computer and use it in GitHub Desktop.
Mongo Find (Un)matching documents in 2 collections
use db1
db.createCollection("scrapped");
db.scrapped.insert(
[
{ sku: "12345" },
{ sku: "23456" },
{ sku: "34567" },
{ sku: "45678" },
{ sku: "67890" }
]
);
db.createCollection("inventory");
db.inventory.insert(
[
{ code: "1234509" },
{ code: "2345698" },
{ code: "3456787" },
{ code: "4567876" },
{ code: "5678965" }
]
);
db.inventory.aggregate(
[
{
$project :
{
code: 1,
sku : { $substr : [ "$code", 0, 5 ] }
}
},
{
$lookup:
{
from: "scrapped",
localField: "sku",
foreignField: "sku",
as: "matching"
}
},
{
$match:
{
matching : { $size : 0 }
}
}
]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment