I am creating a search functionality to search by a few fields, one of which being the id of the document in the collection. When the id is not filled out I am getting the error "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters" as expected since it is trying to convert a blank string to an id. I have tried adding it into the query conditionally but I keep getting errors. Here is the basic idea of the query:
{
$or: [ { name: {$regex: '/*{{companyName.value}}*', $options: 'i'} },
{ _id:
{ "$oid": '{{companyId.value ? companyId.value : '' }}' }
}
]
}
How can I ensure that the section of the query for the id is only run if the user adds an id to the appropriate field?