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 clientDeclaration
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 - A
-
Cleans up internal state.
Declaration
Swift
deinit
-
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.EncodingError
if an error is encountered while encoding the options to BSON.
Declaration
Swift
public func listDatabases(options: ListDatabasesOptions? = nil) throws -> MongoCursor<Document>
Parameters
options
Optional
ListDatabasesOptions
to use when executing the commandReturn Value
A
MongoCursor
overDocument
s 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 databaseReturn Value
a
MongoDatabase
corresponding to the provided database name
-
Disables monitoring for this
MongoClient
. Notifications can be reenabled usingenableMonitoring
.Declaration
Swift
public func disableMonitoring()
-
Enables monitoring for this
MongoClient
for the event type specified, or both types if neither is specified. Sets the destinationNotificationCenter
to the one provided, or the application’s defaultNotificationCenter
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 defaultNotificationCenter
for the application.