MongoClient
public class MongoClient
extension MongoClient: Equatable
A MongoDB Client providing a synchronous API.
-
Encoder whose options are inherited by databases derived from this client.
Declaration
Swift
public var encoder: BSONEncoder { get } -
Decoder whose options are inherited by databases derived from this client.
Declaration
Swift
public var decoder: BSONDecoder { get } -
The read concern set on this client, or nil if one is not set.
Declaration
Swift
public var readConcern: ReadConcern? { get } -
The
ReadPreferenceset 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. For options that are included in both the
MongoConnectionStringand theMongoClientOptionsstruct, the final value is set in descending order of priority: the value specified inMongoClientOptions(if non-nil), the value specified in theMongoConnectionString, or the default value if both are unset.Throws
Throws:
- A
MongoError.InvalidArgumentErrorif the connection string passed in is improperly formatted. - A
MongoError.InvalidArgumentErrorif the connection string specifies the use of TLS but libmongoc was not built with TLS support.
Declaration
Swift
public init(_ connectionString: MongoConnectionString, options: MongoClientOptions? = nil) throwsParameters
connectionStringthe connection string to connect to.
optionsoptional
MongoClientOptionsto use for this client. - A
-
Create a new client connection to a MongoDB server. For options that are included in both the connection string URI and the
MongoClientOptionsstruct, the final value is set in descending order of priority: the value specified inMongoClientOptions(if non-nil), the value specified in the URI, or the default value if both are unset.Throws
Throws:
- A
MongoError.InvalidArgumentErrorif the connection string passed in is improperly formatted. - A
MongoError.InvalidArgumentErrorif the connection string specifies the use of TLS but libmongoc was not built with TLS support.
Declaration
Swift
public convenience init( _ connectionString: String = "mongodb://localhost:27017", options: MongoClientOptions? = nil ) throwsParameters
connectionStringthe connection string to connect to.
optionsoptional
MongoClientOptionsto use for this client. - A
-
Starts a new
ClientSessionwith the provided options.This session must be explicitly as an argument to each command that should be executed as part of the session.
ClientSessions are not thread safe so you must ensure the returned session is not used concurrently for multiple operations.Declaration
Swift
public func startSession(options: ClientSessionOptions? = nil) -> ClientSession -
Starts a new
ClientSessionwith the provided options and passes it to the provided closure. The session must be explicitly passed as an argument to each command within the closure that should be executed as part of the session.The session is only valid within the body of the closure and will be ended after the body completes.
ClientSessions are not thread safe so you must ensure the session is not used concurrently for multiple operations.Throws
Throws:
RuntimeError.CompatibilityErrorif the deployment does not support sessions.
Declaration
Swift
public func withSession<T>( options: ClientSessionOptions? = nil, _ sessionBody: (ClientSession) throws -> T ) rethrows -> TParameters
optionsOptions to use when creating the session.
sessionBodyA closure which takes in a
ClientSessionand returns aT. -
Run the
listDatabasescommand.Throws
Throws:
MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error is encountered while encoding the options to BSON.MongoError.CommandErrorif options.authorizedDatabases is false and the user does not have listDatabases permissions.
Declaration
Swift
public func listDatabases( _ filter: BSONDocument? = nil, options: ListDatabasesOptions? = nil, session: ClientSession? = nil ) throws -> [DatabaseSpecification]Parameters
filterOptional
Documentspecifying a filter that the listed databases must pass. This filter can be based on the “name”, “sizeOnDisk”, “empty”, or “shards” fields of the output.optionsOptional
ListDatabasesOptionsspecifying options for listing databases.sessionOptional
ClientSessionto use when executing this command.Return Value
A
[DatabaseSpecification]containing the databases matching provided criteria. -
Get a list of
MongoDatabases.Throws
Throws:
MongoError.LogicErrorif the provided session is inactive.MongoError.CommandErrorif options.authorizedDatabases is false and the user does not have listDatabases permissions.
Declaration
Swift
public func listMongoDatabases( _ filter: BSONDocument? = nil, options: ListDatabasesOptions? = nil, session: ClientSession? = nil ) throws -> [MongoDatabase]Parameters
filterOptional
Documentspecifying a filter on the names of the returned databases.optionsOptional
ListDatabasesOptionsspecifying options for listing databases.sessionOptional
ClientSessionto use when executing this commandReturn Value
An Array of
MongoDatabases that match the provided filter. -
Get a list of names of databases.
Throws
Throws:
MongoError.LogicErrorif the provided session is inactive.MongoError.CommandErrorif options.authorizedDatabases is false and the user does not have listDatabases permissions.
Declaration
Swift
public func listDatabaseNames( _ filter: BSONDocument? = nil, options: ListDatabasesOptions? = nil, session: ClientSession? = nil ) throws -> [String]Parameters
filterOptional
Documentspecifying a filter on the names of the returned databases.optionsOptional
ListDatabasesOptionsspecifying options for listing databases.sessionOptional
ClientSessionto use when executing this commandReturn Value
A
[String]containing names of databases that match the provided filter. -
Gets a
MongoDatabaseinstance for the given database name. If an option is not specified in the optionalMongoDatabaseOptionsparam, the database will inherit the value from the parent client or the default if the client’s option is not set. To override an option inherited from the client (e.g. a read concern) with the default value, it must be explicitly specified in the options param (e.g. ReadConcern.serverDefault, not nil).Declaration
Swift
public func db(_ name: String, options: MongoDatabaseOptions? = nil) -> MongoDatabaseParameters
namethe name of the database to retrieve
optionsOptional
MongoDatabaseOptionsto use for the retrieved databaseReturn Value
a
MongoDatabasecorresponding to the provided database name -
Starts a
ChangeStreamon aMongoClient. Allows the client to observe all changes in a cluster - excluding system collections and the “config”, “local”, and “admin” databases.Throws
Throws:
MongoError.CommandErrorif an error occurs on the server while creating the change stream.MongoError.InvalidArgumentErrorif the options passed formed an invalid combination.MongoError.InvalidArgumentErrorif the_idfield is projected out of the change stream documents by the pipeline.
See also
SeeAlso:
Note
Supported in MongoDB version 4.0+ only.
Declaration
Swift
public func watch( _ pipeline: [BSONDocument] = [], options: ChangeStreamOptions? = nil, session: ClientSession? = nil ) throws -> ChangeStream<ChangeStreamEvent<BSONDocument>>Parameters
pipelineAn array of aggregation pipeline stages to apply to the events returned by the change stream.
optionsAn optional
ChangeStreamOptionsto use when constructing the change stream.sessionAn optional
ClientSessionto use with this change stream.Return Value
a
ChangeStreamon all collections in all databases in a cluster. -
Starts a
ChangeStreamon aMongoClient. Allows the client to observe all changes in a cluster - excluding system collections and the “config”, “local”, and “admin” databases. Associates the specifiedCodabletypeTwith thefullDocumentfield in theChangeStreamEvents emitted by the returnedChangeStream.Throws
Throws:
MongoError.CommandErrorif an error occurs on the server while creating the change stream.MongoError.InvalidArgumentErrorif the options passed formed an invalid combination.MongoError.InvalidArgumentErrorif the_idfield is projected out of the change stream documents by the pipeline.
See also
SeeAlso:
Note
Supported in MongoDB version 4.0+ only.
Declaration
Swift
public func watch<FullDocType: Codable>( _ pipeline: [BSONDocument] = [], options: ChangeStreamOptions? = nil, session: ClientSession? = nil, withFullDocumentType _: FullDocType.Type ) throws -> ChangeStream<ChangeStreamEvent<FullDocType>>Parameters
pipelineAn array of aggregation pipeline stages to apply to the events returned by the change stream.
optionsAn optional
ChangeStreamOptionsto use when constructing the change stream.sessionAn optional
ClientSessionto use with this change stream.withFullDocumentTypeThe type that the
fullDocumentfield of the emittedChangeStreamEvents will be decoded to.Return Value
A
ChangeStreamon all collections in all databases in a cluster. -
Starts a
ChangeStreamon aMongoClient. Allows the client to observe all changes in a cluster - excluding system collections and the “config”, “local”, and “admin” databases. Associates the specifiedCodabletypeTwith the returnedChangeStream.Throws
Throws:
MongoError.CommandErrorif an error occurs on the server while creating the change stream.MongoError.InvalidArgumentErrorif the options passed formed an invalid combination.MongoError.InvalidArgumentErrorif the_idfield is projected out of the change stream documents by the pipeline.
See also
SeeAlso:
Note
Supported in MongoDB version 4.0+ only.
Declaration
Swift
public func watch<EventType: Codable>( _ pipeline: [BSONDocument] = [], options: ChangeStreamOptions? = nil, session: ClientSession? = nil, withEventType _: EventType.Type ) throws -> ChangeStream<EventType>Parameters
pipelineAn array of aggregation pipeline stages to apply to the events returned by the change stream.
optionsAn optional
ChangeStreamOptionsto use when constructing the change stream.sessionAn optional
ClientSessionto use with this change stream.withEventTypeThe type that the entire change stream response will be decoded to and that will be returned when iterating through the change stream.
Return Value
A
ChangeStreamon all collections in all databases in a cluster. -
Attach a
CommandEventHandlerthat will receiveCommandEvents emitted by this client.Note: the client stores a weak reference to this handler, so it must be kept alive separately in order for it to continue to receive events.
Declaration
Swift
public func addCommandEventHandler<T>(_ handler: T) where T : CommandEventHandler -
Attach a callback that will receive
CommandEvents emitted by this client.Note: if the provided callback captures this client, it must do so weakly. Otherwise, it will constitute a strong reference cycle and potentially result in memory leaks.
Declaration
Swift
public func addCommandEventHandler(_ handlerFunc: @escaping (CommandEvent) -> Void) -
Attach an
SDAMEventHandlerthat will receiveSDAMEvents emitted by this client.Note: the client stores a weak reference to this handler, so it must be kept alive separately in order for it to continue to receive events.
Declaration
Swift
public func addSDAMEventHandler<T>(_ handler: T) where T : SDAMEventHandler -
Attach a callback that will receive
SDAMEvents emitted by this client.Note: if the provided callback captures this client, it must do so weakly. Otherwise, it will constitute a strong reference cycle and potentially result in memory leaks.
Declaration
Swift
public func addSDAMEventHandler(_ handlerFunc: @escaping (SDAMEvent) -> Void) -
Declaration
Swift
public static func == (lhs: MongoClient, rhs: MongoClient) -> Bool
View on GitHub
Install in Dash
MongoClient Class Reference