Hi,
I’m new to MongoDB and I have 2 collections, one called “EN” and another one called “csv_import”. I just need to join these 2 collections using a common field and get the results. For results, I just need the Part number and product id. The 2 collections structure is as follow:
csv_import:
product_id
part_no
vendor_standard
EN: under object “ICECAT-interface.Product”:
@Prod_id
@Name
(these are the main ones but there are other non-important fields, for the sake of this example I include only relevant ones.
just as clarification, “@” is part of the field name
I’m using this to join the two collections:
db.EN.aggregate([
{
$lookup: {
from: 'csv_import',
localField: 'ICECAT-interface.Product.@Prod_id',
foreignField: 'part_no',
as: 'part_number'
}
}]);
Unfortunately, I get an empty array in part_number when I’m expecting just the results that match. Also, how can I specify which fields I want to get back? I thought adding “as: part_number” would be enough but doesn’t seem to be the case
Here’s some collection sample (taken from “EN”)
[{
"_id": "1414",
"ICECAT-interface": {
"@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"@xsi:noNamespaceSchemaLocation": "https://data.icecat.biz/xsd/ICECAT-interface_response.xsd",
"Product": {
"@Code": "1",
"@HighPic": "https://images.icecat.biz/img/norm/high/1414-HP.jpg",
"@HighPicHeight": "400",
"@HighPicSize": "43288",
"@HighPicWidth": "400",
"@ID": "1414",
"@LowPic": "https://images.icecat.biz/img/norm/low/1414-HP.jpg",
"@LowPicHeight": "200",
"@LowPicSize": "17390",
"@LowPicWidth": "200",
"@Name": "C6614NE",
"@IntName": "C6614NE",
"@LocalName": "",
"@Pic500x500": "https://images.icecat.biz/img/gallery_mediums/img_1414_medium_1480667779_072_2323.jpg",
"@Pic500x500Height": "500",
"@Pic500x500Size": "101045",
"@Pic500x500Width": "500",
"@Prod_id": "C6614NE",
Sample collection data taken from “csv_import” collection:
{
"_id": "ObjectId(\"6348339cc6e5c8ce0b7da5a4\")",
"index": 23679,
"product_id": 4019734,
"part_no": "CP-HAR-EP-ADVANCED-REN-1Y",
"vendor_standard": "Check Point"
},
I’ve tried this:
db.EN.aggregate([
{
$lookup: {
from: 'csv_import',
let: {pn:'$ICECAT-interface.Product.@Prod_id'},
pipeline: [{
$match: {
$expr: {
$eq: ["$$pn","$part_no"]
}
}
}],
as: 'part_number_info'
}
}]).pretty();
but yet again, I’m not getting ONLY the matched, I get everything with empty array in part_number_info