Created
January 30, 2018 13:36
-
-
Save yogendra/74b3eab570ad1be755212d887500f836 to your computer and use it in GitHub Desktop.
Mongo Find (Un)matching documents in 2 collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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