How can I use modify and use ‘$and’ in a search query when some items are null?

I need help tweaking the code for this:

const posts = await PostMessage.find({ $or: [ { title }, { tags: { $in: tags.split(',') } } ]});

At the moment, you can filter for posts using a search form which has title and posts. You can leave one empty when you search. It works fine except when you use both. For example searching “hiking” and tagging “Europe” would show posts for both hiking titles AND Europe tags (when I would want the search narrowed down to only show posts with the title hiking and tagged Europe). I know that I can use $and in place of $or but that only returns posts if the user enters both title and tags when I want to give the user the option to leave one blank. Can someone help me write out the code for that?

From what I’ve seen I was wondering if I could do something like this:

Or: {
(And: title not null, tags not null )
(Or title null, tags not null)
(Or title not null, tags null)
)

Is this possible to do? I can’t find a similar example. Thanks for the help!