Authentication is done using by either providing a MongoDB connection string or a ClientSettings object.

MongoDB connection String

Authentication via a connection String requires the username and password on the left side of the hostname like so:

let database = try Database(mongoURL: "mongodb://user:password@hostname")

The username and password need to be URL percent encoded.

Query Parameters

You can enable SSL with the query parameter ssl as described here.

If you need to authenticate to a specific database other than “admin” you can provide an “authSource”:

let database = try Database(mongoURL: "mongodb://user:password@hostname?authSource=myauthdb")

And if you need an authentication mechanism other than SCRAM you can select one using:

let database = try Database(mongoURL: "mongodb://user:password@hostname?authMechanism=SCRAM_SHA_1")

We currently only support “SCRAM_SHA_1” and “MONGODB-CR”. Although other mechanisms can be implemented in the future.