I have aggregation as you can see below. The aggregation works over Invoices table. I just join notifications and then try to filter invoices by notifications.
I would like to get invoices for which notifications have not yet been sent. Other words - give me invoices which have not joined notification with state "SENT", serviceType "EMAIL", type "BILLING_INVOICE" and assigned the invoice. The main problem is the attribute invoice and referencing to _id. I also tried reference by $_id or $$data._id, but nothing works. What is right solution? Thank you.
Example of proper functioning
Invoices
[{_id: 123, order: 333}]
Notifications
[{_id: 345, order: 333, state: SENT, serviceType: EMAIL, type: BILLING_INVOICE, invoice: 123}]
Returned invoices - [{_id: 123}]
Invoices
[{_id: 123, order: 333}]
Notifications
[{_id: 345, order: 333, state: SENT, serviceType: EMAIL, type: BILLING_INVOICE, invoice: 555}]
Returned invoices - []
let invoices = await this.invoiceDao.getModel().aggregate([
// join notifications from order
{
$lookup: {
from: "customer.notifications",
localField: "order",
foreignField: "order",
as: "notifications"
}
},
{
$match: {
"notifications": {
// notification of billing invoice MUST NOT be sent
$not: {
$elemMatch: {
"state": NotificationState.SENT,
"serviceType": NotificationServiceType.EMAIL,
"type": NotificationType.BILLING_INVOICE,
"invoice": "$$_id"
}
}
}
}
}
]);