Using an Array of Values for $match $not $eq in an Aggregation Query

Hello,

I’m currently using the following $match statement in an aggregation query to exclude entries with a booking date that matches the given date. The bookings field is an array of dates. This works fine:

{
    '$match': {
        'bookings': {
            '$not': {
                '$eq': Date('Tue, 09 Aug 2022 16:00:00 GMT')
            }
        }
    }
}

However, I need or would like to pass in an array of dates (that will have been dynamically generated). Is this possible? I’d like to do something like this:

const dates = [
    '2022-09-09T16:00:00.000+00:00',
    '2022-09-08T16:00:00.000+00:00',
    '2022-09-10T16:00:00.000+00:00',
]

{
    '$match': {
        'bookings': {
            '$not': {
                '$eq': Date([dates])
            }
        }
    }
}

What would be the best way of filtering/excluding against an array of data, please?
Cheers,
Matt

Hi,

You can use $nin operator:

{
    '$match': {
        'bookings': {
            '$nin': dates
        }
    }
}

Working example

1 Like

Hi Nenad, brilliant, thank you, $nin was exactly what I was looking for.
Thank you for your help and for the example too!
Have a great day,
Matt

3 Likes

Hi @Matt_Heslington1,

You are welcome. I am glad it helped you!

3 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.