How to specify authentication database and target database separately on mongodb connection uri?

Yes.. Easily!

mongo "mongodb://test:testpwd@localhost/test?authSource=admin"
MongoDB shell version v3.4.6
connecting to: mongodb://test:testpwd@localhost/test?authSource=admin
MongoDB server version: 3.4.6
MongoDB> db
test
MongoDB> 

You put your destination DB at end of "base" string and add authentication db to parameter authSource


Ref:

https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options

You will need to use below format and do not need to use admin database.

mongodb://user:password@localhost/test?authSource=admin

/database Optional. The name of the database to authenticate if the connection string includes authentication credentials in the form of username:password@. If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.

Make sure you have a user in test database. See section 6 of this document.

Enable Auth

Create additional users as needed for your deployment.

The database where you create the user (in this example, test) is that user’s authentication database. Although the user would authenticate to this database, the user can have roles in other databases; i.e. the user’s authentication database does not limit the user’s privileges.

use test
db.createUser(
  {
    user: "myTester",
    pwd: "xyz123",
    roles: [ { role: "readWrite", db: "test" },
             { role: "read", db: "reporting" } ]
  }
)