I need to run a mongoose query that searches the db for records based on a user's access. The access fields will either be ['all']
or an array of valid strings to search for.
The basic structure of the query is below:
let { userid, access } = user;
let allRecords = await db.Record.find({
'organisation': {$in: access.organisations},
'country': {$in: access.countries}
})
However if the user has e.g. access to all organisations, then I don't want to filter on that field. Access to all organisations would be represented by access.organisations === ['all']
. The same is true for country field too.
This could be achieved by some if statements to run the correct query. And that would probably be okay here as I don't have many access fields, but that might expand in the future, so if statements could get messy.
Is there a better way?