Можете ли вы помочь мне с моей проблемой? У меня есть 2 документа: поставщики и консолидация. Мне нужно обновить документ консолидации с полями поставщиков (consolidation.frs_codeclient от supplier.codeclient и consolidation.frs_price from supplier.price) только в том случае, если consolidation.frs_code = supplier.code AND consolidation.etb_code = suppplier.customer_code.
Ниже мой скрипт, но он не работает: (
exports.build = (req, res) => {
console.log("Build consolidation");
Consolidation.aggregate([
{$lookup: {
from: 'suppliers',
as: 'Supplier',
let: {
SupplierCode: '$code',
SupplierCustomerCode: '$customer_code',
SupplierCodeClient: '$codeclient',
SupplierPrice: '$price'
},
pipeline: [
{ $match:
{ $expr: {
$and: [
{ $eq: ['$frs_code', '$$SupplierCode'] },
{ $eq: ['$etb_code', '$$SupplierCustomerCode'] }
]
}}
},
{ $project: {
frs_codeclient: "$$SupplierCodeClient", frs_price: "$$SupplierPrice"
}}
]
}}
])
.then(data => {
res.send({
message: `${data.deletedCount} Consolidations were built successfully!`
});
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while building all Consolidations."
});
});
};
Спасибо за вашу помощь:)
ОБНОВЛЕНИЕ
Я нашел с этим решение:
db.customers.aggregate([
{$lookup: {
from: "suppliers",
let: {
customerCode: "$code",
customerName: "$name",
customerCodePostal: "$codepostal",
customerCity: "$city",
customerPrice: "$price",
customerSupplierCode: "$supplier_code",
customerSupplierCodeClient: "$supplier_codeclient"
},
pipeline: [
{ $match:
{ $expr: {
$and: [
{ $eq: ["$customer_code", "$$customerCode"] },
{ $eq: ["$code", "$$customerSupplierCode"] }
]
}}
},
{ $project: {
_id: 0,
"supplierCode": "$code",
"supplierName": "$name",
"supplierCodeClient": "$codeclient",
"supplierCustomerCode": "$customer_code",
"supplierPrice": "$price",
"customerCode": "$$customerCode",
"customerName": "$$customerName",
"customerCodePostal": "$$customerCodePostal",
"customerCity": "$$customerCity",
"customerPrice": "$$customerPrice",
"customerSupplierCode": "$$customerSupplierCode",
"customerSupplierCodeClient": "$$customerSupplierCodeClient"
}}
],
as: "supplier"
}},
{ $project: {
_id: 0,
"price": 1,
"supplierCode": "$supplier.supplierCode",
"supplierName": "$supplier.supplierName"
}}
])