-
Encoder used by this database for BSON conversions. This encoder’s options are inherited by collections derived from this database.
Declaration
Swift
public let encoder: BSONEncoder
-
The
EventLoop
thisMongoDatabase
is bound to.Declaration
Swift
public let eventLoop: EventLoop?
-
Decoder whose options are inherited by collections derived from this database.
Declaration
Swift
public let decoder: BSONDecoder
-
The name of this database.
Declaration
Swift
public var name: String { get }
-
The
ReadConcern
set on this database, ornil
if one is not set.Declaration
Swift
public let readConcern: ReadConcern?
-
The
ReadPreference
set on this databaseDeclaration
Swift
public let readPreference: ReadPreference
-
The
WriteConcern
set on this database, ornil
if one is not set.Declaration
Swift
public let writeConcern: WriteConcern?
-
Drops this database.
Declaration
Swift
public func drop(options: DropDatabaseOptions? = nil, session: ClientSession? = nil) -> EventLoopFuture<Void>
Return Value
An
EventLoopFuture<Void>
that succeeds when the drop is successful.If the future fails, the error is likely one of the following:
MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this database’s parent client has already been closed.
-
Access a collection within this database. If an option is not specified in the
MongoCollectionOptions
param, 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
name
the name of the collection to get
options
options to set on the returned collection
Return Value
the requested
MongoCollection<Document>
-
Access a collection within this database, and associates the specified
Codable
typeT
with the returnedMongoCollection
. This association only exists in the context of this particularMongoCollection
instance. If an option is not specified in theMongoCollectionOptions
param, 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
name
the name of the collection to get
options
options to set on the returned collection
Return Value
the requested
MongoCollection<T>
-
Creates a collection in this database with the specified options.
Declaration
Swift
public func createCollection( _ name: String, options: CreateCollectionOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<MongoCollection<BSONDocument>>
Parameters
name
a
String
, the name of the collection to createoptions
Optional
CreateCollectionOptions
to use for the collectionsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<MongoCollection<Document>>
. On success, contains the newly created collection.If the future fails, the error is likely one of the following:
MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.InvalidArgumentError
if the options passed in form an invalid combination.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this databases’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
Creates a collection in this database with the specified options, and associates the specified
Codable
typeT
with the returnedMongoCollection
. This association only exists in the context of this particularMongoCollection
instance.Declaration
Swift
public func createCollection<T: Codable>( _ name: String, withType type: T.Type, options: CreateCollectionOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<MongoCollection<T>>
Parameters
name
a
String
, the name of the collection to createoptions
Optional
CreateCollectionOptions
to use for the collectionsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<MongoCollection<T>>
. On success, contains the newly created collection.If the future fails, the error is likely one of the following:
MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.InvalidArgumentError
if the options passed in form an invalid combination.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this databases’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
Lists all the collections in this database.
Warning
If the returned cursor is alive when it goes out of scope, it will leak resources. To ensure the cursor is dead before it leaves scope, invoke
MongoCursor.kill(...)
on it.Declaration
Swift
public func listCollections( _ filter: BSONDocument? = nil, options: ListCollectionsOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<MongoCursor<CollectionSpecification>>
Parameters
filter
a
BSONDocument
, optional criteria to filter results byoptions
Optional
ListCollectionsOptions
to use when executing this commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<MongoCursor<CollectionSpecification>>
containing a cursor over the collections.If the future fails, the error is likely one of the following:
MongoError.InvalidArgumentError
if the options passed are an invalid combination.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this databases’s parent client has already been closed.
-
Gets a list of
MongoCollection
s corresponding to collections in this database.Declaration
Swift
public func listMongoCollections( _ filter: BSONDocument? = nil, options: ListCollectionsOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<[MongoCollection<BSONDocument>]>
Parameters
filter
a
BSONDocument
, optional criteria to filter results byoptions
Optional
ListCollectionsOptions
to use when executing this commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<[MongoCollection<Document>]>
. On success, contains collections that match the provided filter.If the future fails, the error is likely one of the following:
MongoError.InvalidArgumentError
if the options passed are an invalid combination.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this databases’s parent client has already been closed.
-
Gets a list of names of collections in this database.
Declaration
Swift
public func listCollectionNames( _ filter: BSONDocument? = nil, options: ListCollectionsOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<[String]>
Parameters
filter
a
BSONDocument
, optional criteria to filter results byoptions
Optional
ListCollectionsOptions
to use when executing this commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<[String]>
. On success, contains names of collections that match the provided filter.If the future fails, the error is likely one of the following:
MongoError.InvalidArgumentError
if the options passed are an invalid combination.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this databases’s parent client has already been closed.
-
Issues a MongoDB command against this database.
Note
Attempting to specify an API version in this command is considered undefined behavior. API version may only be configured at the
MongoClient
level.Declaration
Swift
public func runCommand( _ command: BSONDocument, options: RunCommandOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<BSONDocument>
Parameters
command
a
BSONDocument
containing the command to issue against the databaseoptions
Optional
RunCommandOptions
to use when executing this commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<Document>
. On success, contains the server response to the command.If the future fails, the error is likely one of the following:
MongoError.InvalidArgumentError
ifrequests
is empty.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this databases’s parent client has already been closed.MongoError.WriteError
if any error occurs while the command was performing a write.MongoError.CommandError
if an error occurs that prevents the command from being performed.EncodingError
if an error occurs while encoding the options to BSON.
-
Starts a
ChangeStream
on a database. Excludes system collections.Warning
If the returned change stream is alive when it goes out of scope, it will leak resources. To ensure the change stream is dead before it leaves scope, invoke
ChangeStream.kill(...)
on it.See also
SeeAlso:
Note
Supported in MongoDB version 4.0+ only.
Declaration
Swift
public func watch( _ pipeline: [BSONDocument] = [], options: ChangeStreamOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<ChangeStream<ChangeStreamEvent<BSONDocument>>>
Parameters
pipeline
An array of aggregation pipeline stages to apply to the events returned by the change stream.
options
An optional
ChangeStreamOptions
to use when constructing the change stream.session
An optional
ClientSession
to use with this change stream.Return Value
An
EventLoopFuture<ChangeStream>
. On success, contains aChangeStream
watching all collections in this database.If the future fails, the error is likely one of the following:
MongoError.CommandError
if an error occurs on the server while creating the change stream.MongoError.InvalidArgumentError
if the options passed formed an invalid combination.MongoError.InvalidArgumentError
if the_id
field is projected out of the change stream documents by the pipeline.
-
Starts a
ChangeStream
on a database. Excludes system collections. Associates the specifiedCodable
typeT
with thefullDocument
field in theChangeStreamEvent
s emitted by the returnedChangeStream
.Warning
If the returned change stream is alive when it goes out of scope, it will leak resources. To ensure the change stream is dead before it leaves scope, invoke
ChangeStream.kill(...)
on it.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 ) -> EventLoopFuture<ChangeStream<ChangeStreamEvent<FullDocType>>>
Parameters
pipeline
An array of aggregation pipeline stages to apply to the events returned by the change stream.
options
An optional
ChangeStreamOptions
to use when constructing the change stream.session
An optional
ClientSession
to use with this change stream.withFullDocumentType
The type that the
fullDocument
field of the emittedChangeStreamEvent
s will be decoded to.Return Value
An
EventLoopFuture<ChangeStream>
. On success, contains aChangeStream
watching all collections in this database.If the future fails, the error is likely one of the following:
MongoError.CommandError
if an error occurs on the server while creating the change stream.MongoError.InvalidArgumentError
if the options passed formed an invalid combination.MongoError.InvalidArgumentError
if the_id
field is projected out of the change stream documents by the pipeline.
-
Starts a
ChangeStream
on a database. Excludes system collections. Associates the specifiedCodable
typeT
with the returnedChangeStream
.Warning
If the returned change stream is alive when it goes out of scope, it will leak resources. To ensure the change stream is dead before it leaves scope, invoke
ChangeStream.kill(...)
on it.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 ) -> EventLoopFuture<ChangeStream<EventType>>
Parameters
pipeline
An array of aggregation pipeline stages to apply to the events returned by the change stream.
options
An optional
ChangeStreamOptions
to use when constructing theChangeStream
.session
An optional
ClientSession
to use with this change stream.withEventType
The type that the entire change stream response will be decoded to and that will be returned when iterating through the change stream.
Return Value
An
EventLoopFuture<ChangeStream>
. On success, contains aChangeStream
watching all collections in this database.If the future fails, the error is likely one of the following:
MongoError.CommandError
if an error occurs on the server while creating the change stream.MongoError.InvalidArgumentError
if the options passed formed an invalid combination.MongoError.InvalidArgumentError
if the_id
field is projected out of the change stream documents by the pipeline.
-
Runs an aggregation framework pipeline against this database for pipeline stages that do not require an underlying collection, such as
$currentOp
and$listLocalSessions
.Warning
If the returned cursor is alive when it goes out of scope, it will leak resources. To ensure the cursor is dead before it leaves scope, invoke
MongoCursor.kill(...)
on it.Declaration
Swift
public func aggregate( _ pipeline: [BSONDocument], options: AggregateOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<MongoCursor<BSONDocument>>
Parameters
pipeline
an
[BSONDocument]
containing the pipeline of aggregation operations to perform.options
Optional
AggregateOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<MongoCursor>
. On success, contains a cursor over the resulting documents.If the future fails, the error is likely one of the following:
MongoError.CommandError
if an error occurs on the server while executing the aggregationMongoError.InvalidArgumentError
if the options passed are an invalid combination.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this database’s parent client has already been closed.EncodingError
if 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
$currentOp
and$listLocalSessions
. Associates the specifiedCodable
typeOutputType
with the returnedMongoCursor
Warning
If the returned cursor is alive when it goes out of scope, it will leak resources. To ensure the cursor is dead before it leaves scope, invoke
MongoCursor.kill(...)
on it.Declaration
Swift
public func aggregate<OutputType: Codable>( _ pipeline: [BSONDocument], options: AggregateOptions? = nil, session: ClientSession? = nil, withOutputType _: OutputType.Type ) -> EventLoopFuture<MongoCursor<OutputType>>
Parameters
pipeline
an
[BSONDocument]
containing the pipeline of aggregation operations to perform.options
Optional
AggregateOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandwithOutputType
the type that each resulting document of the output of the aggregation operation will be decoded to
Return Value
An
EventLoopFuture<MongoCursor>
over the resultingOutputType
sIf the future fails, the error is likely one of the following:
MongoError.CommandError
if an error occurs on the server while executing the aggregationMongoError.InvalidArgumentError
if the options passed are an invalid combination.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this database’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
drop(options:
Asynchronoussession: ) Drops this database.
Declaration
Swift
public func drop(options: DropDatabaseOptions? = nil, session: ClientSession? = nil) async throws
-
createCollection(_:
Asynchronousoptions: session: ) Creates a collection in this database with the specified options.
Throws
Throws:
MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.InvalidArgumentError
if the options passed in form an invalid combination.MongoError.LogicError
if the provided session is inactive.EncodingError
if an error occurs while encoding the options to BSON.
Declaration
Swift
public func createCollection( _ name: String, options: CreateCollectionOptions? = nil, session: ClientSession? = nil ) async throws -> MongoCollection<BSONDocument>
Parameters
name
a
String
, the name of the collection to create.options
Optional
CreateCollectionOptions
to use for the collection.session
Optional
ClientSession
to use when executing this command.Return Value
the newly created
MongoCollection<BSONDocument>
. -
createCollection(_:
AsynchronouswithType: options: session: ) Creates a collection in this database with the specified options, and associates the specified
Codable
typeT
with the returnedMongoCollection
. This association only exists in the context of this particularMongoCollection
instance.Throws
Throws:
MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.InvalidArgumentError
if the options passed in form an invalid combination.MongoError.LogicError
if the provided session is inactive.EncodingError
if 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 ) async throws -> MongoCollection<T>
Parameters
name
a
String
, the name of the collection to create.type
a
Codable
type to associate with the returnedMongoCollection
.options
Optional
CreateCollectionOptions
to use for the collection.session
Optional
ClientSession
to use when executing this command.Return Value
the newly created
MongoCollection<T>
. -
listCollections(_:
Asynchronousoptions: session: ) Lists all the collections in this database.
Throws
Throws:
MongoError.InvalidArgumentError
if the options passed are an invalid combination.MongoError.LogicError
if the provided session is inactive.
Declaration
Swift
public func listCollections( _ filter: BSONDocument? = nil, options: ListCollectionsOptions? = nil, session: ClientSession? = nil ) async throws -> MongoCursor<CollectionSpecification>
Parameters
filter
a
BSONDocument
, optional criteria to filter results by.options
Optional
ListCollectionsOptions
to use when executing this command.session
Optional
ClientSession
to use when executing this command.Return Value
a
MongoCursor
over an array ofCollectionSpecification
s. -
listMongoCollections(_:
Asynchronousoptions: session: ) Gets a list of
MongoCollection
s in this database.Throws
Throws:
MongoError.InvalidArgumentError
if the options passed are an invalid combination.MongoError.LogicError
if the provided session is inactive.
Declaration
Swift
public func listMongoCollections( _ filter: BSONDocument? = nil, options: ListCollectionsOptions? = nil, session: ClientSession? = nil ) async throws -> [MongoCollection<BSONDocument>]
Parameters
filter
a
BSONDocument
, optional criteria to filter results by.options
Optional
ListCollectionsOptions
to use when executing this command.session
Optional
ClientSession
to use when executing this command.Return Value
An array of
MongoCollection
s that match the provided filter. -
listCollectionNames(_:
Asynchronousoptions: session: ) Gets a list of names of collections in this database.
Throws
Throws:
MongoError.InvalidArgumentError
if the options passed are an invalid combination.MongoError.LogicError
if the provided session is inactive.
Declaration
Swift
public func listCollectionNames( _ filter: BSONDocument? = nil, options: ListCollectionsOptions? = nil, session: ClientSession? = nil ) async throws -> [String]
Parameters
filter
a
BSONDocument
, optional criteria to filter results by.options
Optional
ListCollectionsOptions
to use when executing this command.session
Optional
ClientSession
to use when executing this command.Return Value
A
[String]
containing names of collections that match the provided filter. -
runCommand(_:
Asynchronousoptions: session: ) Issues a MongoDB command against this database.
Throws
Throws:
MongoError.InvalidArgumentError
ifrequests
is empty.MongoError.LogicError
if the provided session is inactive.MongoError.WriteError
if any error occurs while the command was performing a write.MongoError.CommandError
if an error occurs that prevents the command from being performed.EncodingError
if 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
MongoClient
level.Declaration
Swift
@discardableResult public func runCommand( _ command: BSONDocument, options: RunCommandOptions? = nil, session: ClientSession? = nil ) async throws -> BSONDocument
Parameters
command
a
BSONDocument
containing the command to issue against the database.options
Optional
RunCommandOptions
to use when executing this command.session
Optional
ClientSession
to use when executing this command.Return Value
a
BSONDocument
containing the server response for the command. -
watch(_:
Asynchronousoptions: session: ) Starts a
ChangeStream
on a database. Excludes system collections.Throws
Throws:
MongoError.CommandError
if an error occurs on the server while creating the change stream.MongoError.InvalidArgumentError
if the options passed formed an invalid combination.MongoError.InvalidArgumentError
if the_id
field 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 ) async throws -> ChangeStream<ChangeStreamEvent<BSONDocument>>
Parameters
pipeline
An array of aggregation pipeline stages to apply to the events returned by the change stream.
options
An optional
ChangeStreamOptions
to use when constructing the change stream.session
An optional
ClientSession
to use with this change stream.Return Value
A
ChangeStream
on all collections in a database. -
watch(_:
Asynchronousoptions: session: withFullDocumentType: ) Starts a
ChangeStream
on a database. Excludes system collections. Associates the specifiedCodable
typeT
with thefullDocument
field in theChangeStreamEvent
s emitted by the returnedChangeStream
.Throws
Throws:
MongoError.CommandError
if an error occurs on the server while creating the change stream.MongoError.InvalidArgumentError
if the options passed formed an invalid combination.MongoError.InvalidArgumentError
if the_id
field 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 ) async throws -> ChangeStream<ChangeStreamEvent<FullDocType>>
Parameters
pipeline
An array of aggregation pipeline stages to apply to the events returned by the change stream.
options
An optional
ChangeStreamOptions
to use when constructing the change stream.session
An optional
ClientSession
to use with this change stream.withFullDocumentType
The type that the
fullDocument
field of the emittedChangeStreamEvent
s will be decoded to.Return Value
A
ChangeStream
on all collections in a database. -
watch(_:
Asynchronousoptions: session: withEventType: ) Starts a
ChangeStream
on a database. Excludes system collections. Associates the specifiedCodable
typeT
with the returnedChangeStream
.Throws
Throws:
MongoError.CommandError
if an error occurs on the server while creating the change stream.MongoError.InvalidArgumentError
if the options passed formed an invalid combination.MongoError.InvalidArgumentError
if the_id
field 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 ) async throws -> ChangeStream<EventType>
Parameters
pipeline
An array of aggregation pipeline stages to apply to the events returned by the change stream.
options
An optional
ChangeStreamOptions
to use when constructing theChangeStream
.session
An optional
ClientSession
to use with this change stream.withEventType
The 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
ChangeStream
on all collections in a database. -
aggregate(_:
Asynchronousoptions: session: ) Runs an aggregation framework pipeline against this database for pipeline stages that do not require an underlying collection, such as
$currentOp
and$listLocalSessions
.Declaration
Swift
public func aggregate( _ pipeline: [BSONDocument], options: AggregateOptions? = nil, session: ClientSession? = nil ) async throws -> MongoCursor<BSONDocument>
Parameters
pipeline
an
[BSONDocument]
containing the pipeline of aggregation operations to perform.options
Optional
AggregateOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
A
MongoCursor
over the resulting documents.Throws:
MongoError.CommandError
if an error occurs on the server while executing the aggregation.MongoError.InvalidArgumentError
if the options passed are an invalid combination.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this database’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
aggregate(_:
Asynchronousoptions: session: withOutputType: ) Runs an aggregation framework pipeline against this database for pipeline stages that do not require an underlying collection, such as
$currentOp
and$listLocalSessions
. Associates the specifiedCodable
typeOutputType
with the returnedMongoCursor
.Declaration
Swift
public func aggregate<OutputType: Codable>( _ pipeline: [BSONDocument], options: AggregateOptions? = nil, session: ClientSession? = nil, withOutputType: OutputType.Type ) async throws -> MongoCursor<OutputType>
Parameters
pipeline
an
[BSONDocument]
containing the pipeline of aggregation operations to perform.options
Optional
AggregateOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.withOutputType
the type that each resulting document of the output of the aggregation operation will be decoded to.
Return Value
A
MongoCursor
over the resultingOutputType
s.Throws:
MongoError.CommandError
if an error occurs on the server while executing the aggregation.MongoError.InvalidArgumentError
if the options passed are an invalid combination.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this database’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.