-
Encoder used by this database for BSON conversions. This encoder’s options are inherited by collections derived from this database.
Declaration
Swift
public var encoder: BSONEncoder { get } -
Decoder whose options are inherited by collections derived from this database.
Declaration
Swift
public var decoder: BSONDecoder { get } -
The name of this database.
Declaration
Swift
public var name: String { get } -
The
ReadConcernset on this database, ornilif one is not set.Declaration
Swift
public var readConcern: ReadConcern? { get } -
The
ReadPreferenceset on this databaseDeclaration
Swift
public var readPreference: ReadPreference { get } -
The
WriteConcernset on this database, ornilif one is not set.Declaration
Swift
public var writeConcern: WriteConcern? { get } -
Drops this database.
Declaration
Swift
public func drop(options: DropDatabaseOptions? = nil, session: ClientSession? = nil) throws -
Access a collection within this database. If an option is not specified in the
MongoCollectionOptionsparam, the collection will inherit the value from the parent database or the default if the db’s option is not set. To override an option inherited from the db (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 collection(_ name: String, options: MongoCollectionOptions? = nil) -> MongoCollection<BSONDocument>Parameters
namethe name of the collection to get
optionsoptions to set on the returned collection
Return Value
the requested
MongoCollection<Document> -
Access a collection within this database, and associates the specified
CodabletypeTwith the returnedMongoCollection. This association only exists in the context of this particularMongoCollectioninstance. If an option is not specified in theMongoCollectionOptionsparam, the collection will inherit the value from the parent database or the default if the db’s option is not set. To override an option inherited from the db (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 collection<T: Codable>( _ name: String, withType _: T.Type, options: MongoCollectionOptions? = nil ) -> MongoCollection<T>Parameters
namethe name of the collection to get
optionsoptions to set on the returned collection
Return Value
the requested
MongoCollection<T> -
Creates a collection in this database with the specified options.
Throws
Throws:
MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options to BSON.
Declaration
Swift
public func createCollection( _ name: String, options: CreateCollectionOptions? = nil, session: ClientSession? = nil ) throws -> MongoCollection<BSONDocument>Parameters
namea
String, the name of the collection to createoptionsOptional
CreateCollectionOptionsto use for the collectionsessionOptional
ClientSessionto use when executing this commandReturn Value
the newly created
MongoCollection<Document> -
Creates a collection in this database with the specified options, and associates the specified
CodabletypeTwith the returnedMongoCollection. This association only exists in the context of this particularMongoCollectioninstance.Throws
Throws:
MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options to BSON.
Declaration
Swift
public func createCollection<T: Codable>( _ name: String, withType type: T.Type, options: CreateCollectionOptions? = nil, session: ClientSession? = nil ) throws -> MongoCollection<T>Parameters
namea
String, the name of the collection to createoptionsOptional
CreateCollectionOptionsto use for the collectionsessionOptional
ClientSessionto use when executing this commandReturn Value
the newly created
MongoCollection<T> -
Lists all the collections in this database.
Throws
Throws:
MongoError.InvalidArgumentErrorif the options passed are an invalid combination.MongoError.LogicErrorif the provided session is inactive.
Declaration
Swift
public func listCollections( _ filter: BSONDocument? = nil, options: ListCollectionsOptions? = nil, session: ClientSession? = nil ) throws -> MongoCursor<CollectionSpecification>Parameters
filtera
Document, optional criteria to filter results byoptionsOptional
ListCollectionsOptionsto use when executing this commandsessionOptional
ClientSessionto use when executing this commandReturn Value
a
MongoCursorover an array ofCollectionSpecifications -
Gets a list of
MongoCollections in this database.Throws
Throws:
MongoError.InvalidArgumentErrorif the options passed are an invalid combination.MongoError.LogicErrorif the provided session is inactive.
Declaration
Swift
public func listMongoCollections( _ filter: BSONDocument? = nil, options: ListCollectionsOptions? = nil, session: ClientSession? = nil ) throws -> [MongoCollection<BSONDocument>]Parameters
filtera
Document, optional criteria to filter results byoptionsOptional
ListCollectionsOptionsto use when executing this commandsessionOptional
ClientSessionto use when executing this commandReturn Value
An array of
MongoCollections that match the provided filter. -
Gets a list of names of collections in this database.
Throws
Throws:
MongoError.InvalidArgumentErrorif the options passed are an invalid combination.MongoError.LogicErrorif the provided session is inactive.
Declaration
Swift
public func listCollectionNames( _ filter: BSONDocument? = nil, options: ListCollectionsOptions? = nil, session: ClientSession? = nil ) throws -> [String]Parameters
filtera
Document, optional criteria to filter results byoptionsOptional
ListCollectionsOptionsto use when executing this commandsessionOptional
ClientSessionto use when executing this commandReturn Value
A
[String]containing names of collections that match the provided filter. -
Issues a MongoDB command against this database.
Throws
Throws:
MongoError.InvalidArgumentErrorifrequestsis empty.MongoError.LogicErrorif the provided session is inactive.MongoError.WriteErrorif any error occurs while the command was performing a write.MongoError.CommandErrorif an error occurs that prevents the command from being performed.EncodingErrorif an error occurs while encoding the options to BSON.
Note
Attempting to specify an API version in this command is considered undefined behavior. API version may only be configured at the
MongoClientlevel.Declaration
Swift
@discardableResult public func runCommand( _ command: BSONDocument, options: RunCommandOptions? = nil, session: ClientSession? = nil ) throws -> BSONDocumentParameters
commanda
Documentcontaining the command to issue against the databaseoptionsOptional
RunCommandOptionsto use when executing this commandsessionOptional
ClientSessionto use when executing this commandReturn Value
a
Documentcontaining the server response for the command -
Starts a
ChangeStreamon a database. Excludes system collections.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 a database. -
Starts a
ChangeStreamon a database. Excludes system collections. 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 a database. -
Starts a
ChangeStreamon a database. Excludes system collections. 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 theChangeStream.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 a database. -
Runs an aggregation framework pipeline against this database for pipeline stages that do not require an underlying collection, such as
$currentOpand$listLocalSessions.Declaration
Swift
public func aggregate( _ pipeline: [BSONDocument], options: AggregateOptions? = nil, session: ClientSession? = nil ) throws -> MongoCursor<BSONDocument>Parameters
pipelinean
[BSONDocument]containing the pipeline of aggregation operations to performoptionsOptional
AggregateOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
A
MongoCursorover the resulting documents.Throws:
MongoError.CommandErrorif an error occurs on the server while executing the aggregationMongoError.InvalidArgumentErrorif the options passed are an invalid combination.MongoError.LogicErrorif the provided session is inactive.MongoError.LogicErrorif this database’s parent client has already been closed.EncodingErrorif an error occurs while encoding the options to BSON.
-
Runs an aggregation framework pipeline against this database for pipeline stages that do not require an underlying collection, such as
$currentOpand$listLocalSessions. Associates the specifiedCodabletypeOutputTypewith the returnedMongoCursorDeclaration
Swift
public func aggregate<OutputType: Codable>( _ pipeline: [BSONDocument], options: AggregateOptions? = nil, session: ClientSession? = nil, withOutputType: OutputType.Type ) throws -> MongoCursor<OutputType>Parameters
pipelinean
[BSONDocument]containing the pipeline of aggregation operations to performoptionsOptional
AggregateOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandwithOutputTypethe type that each resulting document of the output of the aggregation operation will be decoded to
Return Value
A
MongoCursorover the resultingOutputTypesThrows:
MongoError.CommandErrorif an error occurs on the server while executing the aggregationMongoError.InvalidArgumentErrorif the options passed are an invalid combination.MongoError.LogicErrorif the provided session is inactive.MongoError.LogicErrorif this database’s parent client has already been closed.EncodingErrorif an error occurs while encoding the options to BSON.
View on GitHub
Install in Dash
MongoDatabase Structure Reference