How can I refactor the following $let queries

81 Views Asked by At

I am planning to migrate mongodb to aws's documentdb. As per DocDB's documentation, $let queries are not supported natively. So I am looking for options about how can I refactor the following:

let params.status = ["Pending", "Scheduled", "Completed"];
const searchedValues = params.status.map(val => ({ $eq: [ '$$current_status.value', val ] }));

query.$expr = {
    $let: {
        vars: {
            current_status: {
                $arrayElemAt: [
                    '$status_history',
                    -1
                ]
            }
        },
        in: {
            $or: searchedValues
        }
    }
};

Second query:

const searchedValues = params.status.map(val => ({ $eq: [ '$$current_status.value', val ] }));

query.$expr = {
    $let: {
        vars: {
            current_status: {
                $arrayElemAt: [
                    '$status_history',
                    -1
                ]
            }
        },
        in: {
            $or: searchedValues
        }
    }
};

status_history is an array.

I have not worked with mongodb enough to be able to refactor this. Any help will be appreciated.

1

There are 1 best solutions below

0
On

The workaround I see is to assign the current_status variable in code, as the result of the { $arrayElemAt: [ ‘$status_history’, -1 ]} query, then do the $or query. Yes, that will be 2 queries instead of one, but it can be a reasonable trade off.