MongoClient

public class MongoClient

A MongoDB Client.

  • Encoder whose options are inherited by databases derived from this client.

    Declaration

    Swift

    public let encoder: BSONEncoder
  • Decoder whose options are inherited by databases derived from this client.

    Declaration

    Swift

    public let decoder: BSONDecoder
  • The read concern set on this client, or nil if one is not set.

    Declaration

    Swift

    public var readConcern: ReadConcern? { get }
  • The ReadPreference set on this client

    Declaration

    Swift

    public var readPreference: ReadPreference { get }
  • The write concern set on this client, or nil if one is not set.

    Declaration

    Swift

    public var writeConcern: WriteConcern? { get }
  • Create a new client connection to a MongoDB server.

    Throws

    Throws:

    • A UserError.invalidArgumentError if the connection string passed in is improperly formatted.
    • A UserError.invalidArgumentError if the connection string specifies the use of TLS but libmongoc was not built with TLS support.

    Declaration

    Swift

    public init(_ connectionString: String = "mongodb://localhost:27017", options: ClientOptions? = nil) throws

    Parameters

    connectionString

    the connection string to connect to.

    options

    optional ClientOptions to use for this client

  • Starts a new ClientSession with the provided options.

    Throws

    • RuntimeError.compatibilityError if the deployment does not support sessions.

    Declaration

    Swift

    public func startSession(options: ClientSessionOptions? = nil) throws -> ClientSession
  • Starts a new ClientSession with the provided options and passes it to the provided closure. The session is only valid within the body of the closure and will be ended after the body completes.

    Throws

    • RuntimeError.compatibilityError if the deployment does not support sessions.

    Declaration

    Swift

    public func withSession<T>(options: ClientSessionOptions? = nil,
                               _ sessionBody: (ClientSession) throws -> T) throws -> T
  • Closes the client.

    Declaration

    Swift

    public func close()
  • Get a list of databases.

    Throws

    Throws:

    • UserError.invalidArgumentError if the options passed are an invalid combination.
    • UserError.logicError if the provided session is inactive.
    • EncodingError if an error is encountered while encoding the options to BSON.

    Declaration

    Swift

    public func listDatabases(options: ListDatabasesOptions? = nil,
                              session: ClientSession? = nil) throws -> MongoCursor<Document>

    Parameters

    options

    Optional ListDatabasesOptions to use when executing the command

    Return Value

    A MongoCursor over Documents describing the databases matching provided criteria

  • Gets a MongoDatabase instance for the given database name.

    Declaration

    Swift

    public func db(_ name: String, options: DatabaseOptions? = nil) -> MongoDatabase

    Parameters

    name

    the name of the database to retrieve

    options

    Optional DatabaseOptions to use for the retrieved database

    Return Value

    a MongoDatabase corresponding to the provided database name

  • Disables monitoring for this MongoClient. Notifications can be reenabled using enableMonitoring.

    Declaration

    Swift

    public func disableMonitoring()
  • Enables monitoring for this MongoClient for the event type specified, or both types if neither is specified. Sets the destination NotificationCenter to the one provided, or the application’s default NotificationCenter if one is not specified.

    Declaration

    Swift

    public func enableMonitoring(forEvents eventType: MongoEventType? = nil,
                                 usingCenter center: NotificationCenter = NotificationCenter.default)

    Parameters

    forEvents

    A MongoEventType? to enable monitoring for, defaulting to nil. If unspecified, monitoring will be enabled for both .commandMonitoring and .serverMonitoring events.

    usingCenter

    A NotificationCenter that event notifications should be posted to, defaulting to the default NotificationCenter for the application.