You could create an index on dueDate on Invoices as well as the invoiceId on Payments. If your query includes multiple fields, then you want a compound index on exactly the fields that will be on that query. If you have another query with another set of fields, create another index on that too.
Note that you can’t make indexes that span multiple collections, so you can’t make an index that includes dueDate on Invoices and also invoiceId on Payments.
If you want to find the sum of payment amounts (in order to compare them to the invoice amounts), you could do an aggregation on Payments, which is designed to be faster as a Mongo internal than doing the sum after a find().fetch():
async function getPaymentSums (listOfInvoiceIds) {
return await Payments.rawCollection().aggregate([
// in this scenario, invoiceId should be indexed
{ $match: { invoiceID: {$in: listOfInvoiceIds} } },
{ $group: { _id: "$invoiceId", total: { $sum: "$amount" } } }
])
Thinking of architecture, if you can run a query at the time of insert/update (as you mention) by which you can find out an invoice is paid, isn’t that what would be the result every subsequent time that query runs, and wouldn’t you want to save that information on Invoices…?