Hello,
I have an aggregation query like the below on a collection of bookings, bookings have a days property which is an array of days, each day has a start and end date. The aggregation query returns all bookings where some of the days fall between a start and end date (start, end dates come from request payload). Every time this query is ran however I receieve an alert that Query Targeting: Scanned Objects / Returned has gone above 1000 but I canno’t seem to get the right index’s to help prevent this, I have also tried lots of variations of the below query all with no success.
const match = {
status: 1,
customer: new Types.ObjectId(customer),
};
// add filters to match query if they exist in request payload
if (userFilter) match.assignee = userFilter.assignee;
if (clientFilter) match.client = clientFilter.client;
if (typeFilter) match.type = typeFilter.type;
Booking.aggregate([
{
$match: {
...match,
days: {
$elemMatch: {
// filter bookings by start and end date
start: {
$gte: new Date(start).toISOString(),
$lte: new Date(end).toISOString(),
},
},
},
},
},
{
$sort: { createdAt: 1 },
}
])
Any help would be greatly appreciated!