mahi-man
mahi-man

Reputation: 4686

MongoDB Atlas search filter range with an OR criteria

I would like to do a range search on a date using MongoDB Atlas Search, but I also need to include search results where there is no date at all. This is how I would do this in a $match stage of an aggregation pipeline:

$match: {
    $or: [
        {
            publish_start_date: {
                $lte: todayDateTime,
            },
        },
        {
            publish_start_date: undefined,
        },
    ],
}

Here is the equivalent of the first part in an atlas $search

$search: {
    index: "iapp-component-search",
    compound: {
        filter: [
            {
                range: {
                    path: "publish_start_date",
                    lte: todayDateTime,
                },
            },
        ]
    }
}

I know I can simply add the $match stage to the aggregation pipeline after the $search, but the Atlas docs say this is not good for performance, so I would like to include it in the filter of the $search.

How would I add an "OR" clause to also include documents with a publish_start_date of undefined in the $search stage?

Upvotes: 1

Views: 957

Answers (1)

D. SM
D. SM

Reputation: 14520

For all documents that don't have a start date, write a placeholder value into the date e.g. 0000-00-00. If the missing date means something for your application, duplicate the data + placeholder into a new field.

Upvotes: 1

Related Questions