A query is simply a JSON document, or js object if you prefer that terminology.
So, nothing stops you from using if-statements to build it dynamically rather than having a static structure. The structure of the object in your code is static because the field name will always be part of the query.
query = { ["owner.id"] : slash.user.id , division }
if ( what ever condition you want on teamName )
{
// Dynamically update the query object with teamName
query.name = teamName
}
const team = await teams.findOne( query )
Not that I have not used $and because it is not necessary.
See a somewhat related thread How to dynamically add conditions or filters to aggregate query - #2 by steevej.
The course M220JS is full of dynamic queries.