Lets say we have postgres and mongodb server and we sharded.

How does the database knows the specific database server to query a certain record.
or do we have to implement the logic in application layer

Does it differs for sql and no sql database