MongoCollection
public struct MongoCollection<T> where T : Decodable, T : Encodable
A MongoDB collection.
-
The namespace for this collection.
Declaration
Swift
public let namespace: MongoNamespace
-
Encoder used by this collection for BSON conversions. (e.g. converting
CollectionType
s, indexes, and options to documents).Declaration
Swift
public let encoder: BSONEncoder
-
Decoder used by this collection for BSON conversions (e.g. converting documents to
CollectionType
s).Declaration
Swift
public let decoder: BSONDecoder
-
The
EventLoop
thisMongoCollection
is bound to.Declaration
Swift
public let eventLoop: EventLoop?
-
A
Codable
type associated with thisMongoCollection
instance. This allowsCollectionType
values to be directly inserted into and retrieved from the collection, by encoding/decoding them using theBSONEncoder
andBSONDecoder
. The strategies to be used by the encoder and decoder for certain types can be configured by setting the coding strategies on the options used to create this collection instance. The default strategies are inherited from those set on the database this collection derived from.This type association only exists in the context of this particular
MongoCollection
instance. It is the responsibility of the user to ensure that any data already stored in the collection was encoded from this same type and according to the coding strategies set on this instance.Declaration
Swift
public typealias CollectionType = T
-
The name of this collection.
Declaration
Swift
public var name: String { get }
-
The
ReadConcern
set on this collection, ornil
if one is not set.Declaration
Swift
public let readConcern: ReadConcern?
-
The
ReadPreference
set on this collection.Declaration
Swift
public let readPreference: ReadPreference
-
The
WriteConcern
set on this collection, or nil if one is not set.Declaration
Swift
public let writeConcern: WriteConcern?
-
Drops this collection from its parent database.
Declaration
Swift
public func drop(options: DropCollectionOptions? = 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 collection’s parent client has already been closed.
-
Renames this collection on the server. This method will return a handle to the renamed collection. The handle which this method is invoked on will continue to refer to the old collection, which will then be empty. The server will throw an error if the new name matches an existing collection unless the
dropTarget
option is set to true.Note: This method is not supported on sharded collections.
Declaration
Swift
public func renamed( to newName: String, options: RenameCollectionOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<MongoCollection>
Parameters
to
A
String
, the new name for the collectionoptions
Optional
RenameCollectionOptions
to use for the collectionsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<MongoCollection>
evaluating to a copy of the targetMongoCollection
with the new name.If the future evaluates to an error, it 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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
drop(options:
Asynchronoussession: ) Drops this collection from its parent database.
Declaration
Swift
public func drop(options: DropCollectionOptions? = nil, session: ClientSession? = nil) async throws
Parameters
options
An optional
DropCollectionOptions
to use when executing this command.session
An optional
ClientSession
to use when executing this command. -
renamed(to:
Asynchronousoptions: session: ) Renames this collection on the server. This method will return a handle to the renamed collection. The handle which this method is invoked on will continue to refer to the old collection, which will then be empty. The server will throw an error if the new name matches an existing collection unless the
dropTarget
option is set to true.Note: This method is not supported on sharded collections.
Declaration
Swift
public func renamed( to newName: String, options: RenameCollectionOptions? = nil, session: ClientSession? = nil ) async throws -> MongoCollection
Parameters
to
A
String
, the new name for the collection.options
Optional
RenameCollectionOptions
to use for the collection.session
Optional
ClientSession
to use when executing this command.Return Value
A copy of the target
MongoCollection
with the new name.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.MongoError.LogicError
if this collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
watch(_:
Asynchronousoptions: session: ) Starts a
ChangeStream
on a collection. TheCollectionType
will be associated with thefullDocument
field inChangeStreamEvent
s emitted by the returnedChangeStream
. The server will return an error if this method is called on a system collection.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.
Declaration
Swift
public func watch( _ pipeline: [BSONDocument] = [], options: ChangeStreamOptions? = nil, session: ClientSession? = nil ) async throws -> ChangeStream<ChangeStreamEvent<CollectionType>>
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 a specific collection. -
watch(_:
Asynchronousoptions: session: withFullDocumentType: ) Starts a
ChangeStream
on a collection. Associates the specifiedCodable
typeT
with thefullDocument
field in theChangeStreamEvent
s emitted by the returnedChangeStream
. The server will return an error if this method is called on a system collection.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.
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 a specific collection. -
watch(_:
Asynchronousoptions: session: withEventType: ) Starts a
ChangeStream
on a collection. Associates the specifiedCodable
typeT
with the returnedChangeStream
. The server will return an error if this method is called on a system collection.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.
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 the change stream.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 a specific collection. -
findOneAndDelete(_:
Asynchronousoptions: session: ) Finds a single document and deletes it, returning the original.
Throws
Throws:
MongoError.InvalidArgumentError
if any of the provided options are invalid.MongoError.LogicError
if the provided session is inactive.MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.WriteError
if an error occurs while executing the command.DecodingError
if the deleted document cannot be decoded to aCollectionType
value.
Declaration
Swift
@discardableResult public func findOneAndDelete( _ filter: BSONDocument, options: FindOneAndDeleteOptions? = nil, session: ClientSession? = nil ) async throws -> CollectionType?
Parameters
filter
a
BSONDocument
representing the match criteria.options
Optional
FindOneAndDeleteOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
The deleted document, represented as a
CollectionType
, ornil
if no document was deleted. -
Finds a single document and replaces it, returning either the original or the replaced document.
Throws
Throws:
MongoError.InvalidArgumentError
if any of the provided options are invalid.MongoError.LogicError
if the provided session is inactive.MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.WriteError
if an error occurs while executing the command.DecodingError
if the replaced document cannot be decoded to aCollectionType
value.EncodingError
ifreplacement
cannot be encoded to aBSONDocument
.
Declaration
Swift
@discardableResult public func findOneAndReplace( filter: BSONDocument, replacement: CollectionType, options: FindOneAndReplaceOptions? = nil, session: ClientSession? = nil ) async throws -> CollectionType?
Parameters
filter
a
BSONDocument
representing the match criteria.replacement
a
CollectionType
to replace the found document.options
Optional
FindOneAndReplaceOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
A
CollectionType
, representing either the original document or its replacement, depending on selected options, ornil
if there was no match. -
findOneAndUpdate(filter:
Asynchronousupdate: options: session: ) Finds a single document and updates it, returning either the original or the updated document.
Throws
Throws:
MongoError.InvalidArgumentError
if any of the provided options are invalid.MongoError.LogicError
if the provided session is inactive.MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.WriteError
if an error occurs while executing the command.DecodingError
if the updated document cannot be decoded to aCollectionType
value.
Declaration
Swift
@discardableResult public func findOneAndUpdate( filter: BSONDocument, update: BSONDocument, options: FindOneAndUpdateOptions? = nil, session: ClientSession? = nil ) async throws -> CollectionType?
Parameters
filter
a
BSONDocument
representing the match criteria.update
a
BSONDocument
containing updates to apply.options
Optional
FindOneAndUpdateOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
A
CollectionType
representing either the original or updated document, depending on selected options, ornil
if there was no match. -
findOneAndUpdate(filter:
Asynchronouspipeline: options: session: ) Finds a single document and updates it, returning either the original or the updated document.
Throws
Throws:
MongoError.InvalidArgumentError
if any of the provided options are invalid.MongoError.LogicError
if the provided session is inactive.MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.WriteError
if an error occurs while executing the command.DecodingError
if the updated document cannot be decoded to aCollectionType
value.
Declaration
Swift
@discardableResult public func findOneAndUpdate( filter: BSONDocument, pipeline: [BSONDocument], options: FindOneAndUpdateOptions? = nil, session: ClientSession? = nil ) async throws -> CollectionType?
Parameters
filter
a
BSONDocument
representing the match criteria.pipeline
an array of
BSONDocument
containing the aggregation pipeline to apply.options
Optional
FindOneAndUpdateOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
A
CollectionType
representing either the original or updated document, depending on selected options, ornil
if there was no match. -
createIndex(_:
AsynchronousindexOptions: options: session: ) Creates an index over the collection for the provided keys with the provided options.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the write.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 index specification or options.
Declaration
Swift
@discardableResult public func createIndex( _ keys: BSONDocument, indexOptions: IndexOptions? = nil, options: CreateIndexOptions? = nil, session: ClientSession? = nil ) async throws -> String
Parameters
keys
a
BSONDocument
specifing the keys for the index.indexOptions
Optional
IndexOptions
to use for the index.options
Optional
CreateIndexOptions
to use for the command.session
Optional
ClientSession
to use when executing this command.Return Value
The name of the created index.
-
createIndex(_:
Asynchronousoptions: session: ) Creates an index over the collection for the provided keys with the provided options.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the write.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 index specification or options.
Declaration
Swift
@discardableResult public func createIndex( _ model: IndexModel, options: CreateIndexOptions? = nil, session: ClientSession? = nil ) async throws -> String
Parameters
model
An
IndexModel
representing the keys and options for the index.options
Optional
CreateIndexOptions
to use for the command.session
Optional
ClientSession
to use when executing this command.Return Value
The name of the created index.
-
createIndexes(_:
Asynchronousoptions: session: ) Creates multiple indexes in the collection.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the write.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 index specifications or options.
Declaration
Swift
@discardableResult public func createIndexes( _ models: [IndexModel], options: CreateIndexOptions? = nil, session: ClientSession? = nil ) async throws -> [String]
Parameters
models
An
[IndexModel]
specifying the indexes to create.options
Optional
CreateIndexOptions
to use for the command.session
Optional
ClientSession
to use when executing this command.Return Value
An
[String]
containing the names of all the indexes that were created. -
dropIndex(_:
Asynchronousoptions: session: ) Drops a single index from the collection by the index name.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the command.MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.InvalidArgumentError
if the options passed in form an invalid combination.EncodingError
if an error occurs while encoding the options.
Declaration
Swift
public func dropIndex( _ name: String, options: DropIndexOptions? = nil, session: ClientSession? = nil ) async throws
Parameters
name
The name of the index to drop.
options
Optional
DropIndexOptions
to use for the command.session
Optional
ClientSession
to use when executing this command. -
dropIndex(_:
Asynchronousoptions: session: ) Attempts to drop a single index from the collection given the keys describing it.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the command.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.
Declaration
Swift
public func dropIndex( _ keys: BSONDocument, options: DropIndexOptions? = nil, session: ClientSession? = nil ) async throws
Parameters
keys
a
BSONDocument
containing the keys for the index to drop.options
Optional
DropIndexOptions
to use for the command.session
Optional
ClientSession
to use when executing this command. -
dropIndex(_:
Asynchronousoptions: session: ) Attempts to drop a single index from the collection given an
IndexModel
describing it.Throws
Throws:
MongoError.WriteError
if an error occurs while performing the command.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.
Declaration
Swift
public func dropIndex( _ model: IndexModel, options: DropIndexOptions? = nil, session: ClientSession? = nil ) async throws
Parameters
model
An
IndexModel
describing the index to drop.options
Optional
DropIndexOptions
to use for the command.session
Optional
ClientSession
to use when executing this command. -
dropIndexes(options:
Asynchronoussession: ) Drops all indexes in the collection.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the command.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.
Declaration
Swift
public func dropIndexes( options: DropIndexOptions? = nil, session: ClientSession? = nil ) async throws
Parameters
options
Optional
DropIndexOptions
to use for the command.session
Optional
ClientSession
to use when executing this command. -
listIndexes(session:
Asynchronous) Retrieves a list of the indexes currently on this collection.
Throws
MongoError.LogicError
if the provided session is inactive.Declaration
Swift
public func listIndexes(session: ClientSession? = nil) async throws -> MongoCursor<IndexModel>
Parameters
session
Optional
ClientSession
to use when executing this command.Return Value
A
MongoCursor
over theIndexModel
s. -
listIndexNames(session:
Asynchronous) Retrieves a list of names of the indexes currently on this collection.
Throws
MongoError.LogicError
if the provided session is inactive.Declaration
Swift
public func listIndexNames(session: ClientSession? = nil) async throws -> [String]
Parameters
session
Optional
ClientSession
to use when executing this command.Return Value
A
MongoCursor
over the index names. -
find(_:
Asynchronousoptions: session: ) Finds the documents in this collection which match the provided filter.
Throws
Throws:
MongoError.InvalidArgumentError
if the options passed are 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 find( _ filter: BSONDocument = [:], options: FindOptions? = nil, session: ClientSession? = nil ) async throws -> MongoCursor<CollectionType>
Parameters
filter
A
BSONDocument
that should match the query.options
Optional
FindOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
A
MongoCursor
over the resultingBSONDocument
s. -
findOne(_:
Asynchronousoptions: session: ) Finds a single document in this collection that matches the provided filter.
Throws
Throws:
MongoError.InvalidArgumentError
if the options passed are 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 findOne( _ filter: BSONDocument = [:], options: FindOneOptions? = nil, session: ClientSession? = nil ) async throws -> T?
Parameters
filter
A
BSONDocument
that should match the query.options
Optional
FindOneOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
the resulting
BSONDocument
, or nil if there is no match. -
aggregate(_:
Asynchronousoptions: session: ) Runs an aggregation framework pipeline against this collection.
Throws
Throws:
MongoError.InvalidArgumentError
if the options passed are 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 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 resultingBSONDocument
s. -
aggregate(_:
Asynchronousoptions: session: withOutputType: ) Runs an aggregation framework pipeline against this collection. Associates the specified
Codable
typeOutputType
with the returnedMongoCursor
.Throws
Throws:
MongoError.InvalidArgumentError
if the options passed are 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 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. -
countDocuments(_:
Asynchronousoptions: session: ) Counts the number of documents in this collection matching the provided filter. Note that an empty filter will force a scan of the entire collection. For a fast count of the total documents in a collection see
estimatedDocumentCount
.Declaration
Swift
public func countDocuments( _ filter: BSONDocument = [:], options: CountDocumentsOptions? = nil, session: ClientSession? = nil ) async throws -> Int
Parameters
filter
a
BSONDocument
, the filter that documents must match in order to be counted.options
Optional
CountDocumentsOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
The count of the documents that matched the filter.
-
estimatedDocumentCount(options:
Asynchronous) Gets an estimate of the count of documents in this collection using collection metadata. This operation cannot be used in a transaction.
Declaration
Swift
public func estimatedDocumentCount(options: EstimatedDocumentCountOptions? = nil) async throws -> Int
Parameters
options
Optional
EstimatedDocumentCountOptions
to use when executing the command.Return Value
an estimate of the count of documents in this collection.
-
distinct(fieldName:
Asynchronousfilter: options: session: ) Finds the distinct values for a specified field across the collection.
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 distinct( fieldName: String, filter: BSONDocument = [:], options: DistinctOptions? = nil, session: ClientSession? = nil ) async throws -> [BSON]
Parameters
fieldName
The field for which the distinct values will be found.
filter
a
BSONDocument
representing the filter documents must match in order to be considered for the operation.options
Optional
DistinctOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
A
[BSON]
containing the distinct values for the specified criteria. -
insertOne(_:
Asynchronousoptions: session: ) Encodes the provided value to BSON and inserts it. If the value is missing an identifier, one will be generated for it.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the command.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 theCollectionType
to BSON.
Declaration
Swift
@discardableResult public func insertOne( _ value: CollectionType, options: InsertOneOptions? = nil, session: ClientSession? = nil ) async throws -> InsertOneResult?
Parameters
value
A
CollectionType
value to encode and insert.options
Optional
InsertOneOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
An
InsertOneResult
, ornil
if the write concern is unacknowledged. -
insertMany(_:
Asynchronousoptions: session: ) Encodes the provided values to BSON and inserts them. If any values are missing identifiers, the driver will generate them.
Throws
Throws:
MongoError.BulkWriteError
if an error occurs while performing any of the writes.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 theCollectionType
or options to BSON.
Declaration
Swift
@discardableResult public func insertMany( _ values: [CollectionType], options: InsertManyOptions? = nil, session: ClientSession? = nil ) async throws -> InsertManyResult?
Parameters
values
The
CollectionType
values to insert.options
optional
InsertManyOptions
to use while executing the operation.session
Optional
ClientSession
to use when executing this command.Return Value
An
InsertManyResult
, ornil
if the write concern is unacknowledged. -
replaceOne(filter:
Asynchronousreplacement: options: session: ) Replaces a single document matching the provided filter in this collection.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the command.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 theCollectionType
or options to BSON.
Declaration
Swift
@discardableResult public func replaceOne( filter: BSONDocument, replacement: CollectionType, options: ReplaceOptions? = nil, session: ClientSession? = nil ) async throws -> UpdateResult?
Parameters
filter
A
BSONDocument
representing the match criteria.replacement
The replacement value, a
CollectionType
value to be encoded and inserted.options
Optional
ReplaceOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
An
UpdateResult
, ornil
if the write concern is unacknowledged. -
updateOne(filter:
Asynchronousupdate: options: session: ) Updates a single document matching the provided filter in this collection.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the command.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
@discardableResult public func updateOne( filter: BSONDocument, update: BSONDocument, options: UpdateOptions? = nil, session: ClientSession? = nil ) async throws -> UpdateResult?
Parameters
filter
A
BSONDocument
representing the match criteria.update
A
BSONDocument
representing the update to be applied to a matching document.options
Optional
UpdateOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
An
UpdateResult
, ornil
if the write concern is unacknowledged. -
updateOne(filter:
Asynchronouspipeline: options: session: ) Updates a single document matching the provided filter in this collection.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the command.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
@discardableResult public func updateOne( filter: BSONDocument, pipeline: [BSONDocument], options: UpdateOptions? = nil, session: ClientSession? = nil ) async throws -> UpdateResult?
Parameters
filter
A
BSONDocument
representing the match criteria.pipeline
A
[BSONDocument]
representing the aggregation pipeline to be applied to a matching document.options
Optional
UpdateOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
An
UpdateResult
, ornil
if the write concern is unacknowledged. -
updateMany(filter:
Asynchronousupdate: options: session: ) Updates multiple documents matching the provided filter in this collection.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the command.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
@discardableResult public func updateMany( filter: BSONDocument, update: BSONDocument, options: UpdateOptions? = nil, session: ClientSession? = nil ) async throws -> UpdateResult?
Parameters
filter
A
BSONDocument
representing the match criteria.update
A
BSONDocument
representing the update to be applied to matching documents.options
Optional
UpdateOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
An
UpdateResult
, ornil
if the write concern is unacknowledged. -
updateMany(filter:
Asynchronouspipeline: options: session: ) Updates multiple documents matching the provided filter in this collection.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the command.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
@discardableResult public func updateMany( filter: BSONDocument, pipeline: [BSONDocument], options: UpdateOptions? = nil, session: ClientSession? = nil ) async throws -> UpdateResult?
Parameters
filter
A
BSONDocument
representing the match criteria.pipeline
A
[BSONDocument]
representing the aggregation pipeline to be applied to matching documents.options
Optional
UpdateOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
An
UpdateResult
, ornil
if the write concern is unacknowledged. -
deleteOne(_:
Asynchronousoptions: session: ) Deletes a single matching document from the collection.
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the command.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
@discardableResult public func deleteOne( _ filter: BSONDocument, options: DeleteOptions? = nil, session: ClientSession? = nil ) async throws -> DeleteResult?
Parameters
filter
A
BSONDocument
representing the match criteria.options
Optional
DeleteOptions
to use when executing the command.session
Optional
ClientSession
to use when executing this command.Return Value
A
DeleteResult
, ornil
if the write concern is unacknowledged. -
deleteMany(_:
Asynchronousoptions: session: ) Deletes multiple documents
Throws
Throws:
MongoError.WriteError
if an error occurs while performing the command.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
@discardableResult public func deleteMany( _ filter: BSONDocument, options: DeleteOptions? = nil, session: ClientSession? = nil ) async throws -> DeleteResult?
Parameters
filter
Document representing the match criteria
options
Optional
DeleteOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
A
DeleteResult
, ornil
if the write concern is unacknowledged. -
bulkWrite(_:
Asynchronousoptions: session: ) Execute multiple write operations.
Throws
Throws:
MongoError.InvalidArgumentError
ifrequests
is empty.MongoError.LogicError
if the provided session is inactive.MongoError.BulkWriteError
if any error occurs while performing the writes. This includes errors that would typically be thrown asRuntimeError
s orMongoError.CommandError
s elsewhere.EncodingError
if an error occurs while encoding theCollectionType
or the options to BSON.
Declaration
Swift
@discardableResult public func bulkWrite( _ requests: [WriteModel<T>], options: BulkWriteOptions? = nil, session: ClientSession? = nil ) async throws -> BulkWriteResult?
Parameters
requests
a
[WriteModel]
containing the writes to perform.options
optional
BulkWriteOptions
to use while executing the operation.session
Optional
ClientSession
to use when executing this commandReturn Value
a
BulkWriteResult
, ornil
if the write concern is unacknowledged. -
Execute multiple write operations.
Declaration
Swift
public func bulkWrite( _ requests: [WriteModel<T>], options: BulkWriteOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<BulkWriteResult?>
Parameters
requests
a
[WriteModel]
containing the writes to perform.options
optional
BulkWriteOptions
to use while executing the operation.session
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<BulkWriteResult?>
. On success, the future contains either aBulkWriteResult
, or containsnil
if the write concern is unacknowledged.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 collection’s parent client has already been closed.MongoError.BulkWriteError
if any error occurs while performing the writes. This includes errors that would typically be propagated asRuntimeError
s orMongoError.CommandError
s elsewhere.EncodingError
if an error occurs while encoding theCollectionType
or the options to BSON.
-
Starts a
ChangeStream
on this collection. TheCollectionType
will be associated with thefullDocument
field inChangeStreamEvent
s emitted by the returnedChangeStream
. The server will return an error if this method is called on a system collection.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.Declaration
Swift
public func watch( _ pipeline: [BSONDocument] = [], options: ChangeStreamOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<ChangeStream<ChangeStreamEvent<CollectionType>>>
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 this collection.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 this collection. Associates the specifiedCodable
typeT
with thefullDocument
field in theChangeStreamEvent
s emitted by the returnedChangeStream
. The server will return an error if this method is called on a system collection.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.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 this collection.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 this collection. Associates the specifiedCodable
typeT
with the returnedChangeStream
. The server will return an error if this method is called on a system collection.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.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 the change stream.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 this collection.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.
-
Finds a single document and deletes it, returning the original.
Declaration
Swift
public func findOneAndDelete( _ filter: BSONDocument, options: FindOneAndDeleteOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<CollectionType?>
Parameters
filter
BSONDocument
representing the match criteriaoptions
Optional
FindOneAndDeleteOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<CollectionType?>
. On success, contains either the deleted document, represented as aCollectionType
, or containsnil
if no document was deleted.If the future fails, the error is likely one of the following:
MongoError.InvalidArgumentError
if any of the provided options are invalid.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this collection’s parent client has already been closed.MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.WriteError
if an error occurs while executing the command.DecodingError
if the deleted document cannot be decoded to aCollectionType
value.
-
Finds a single document and replaces it, returning either the original or the replaced document.
Declaration
Swift
public func findOneAndReplace( filter: BSONDocument, replacement: CollectionType, options: FindOneAndReplaceOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<CollectionType?>
Parameters
filter
BSONDocument
representing the match criteriareplacement
a
CollectionType
to replace the found documentoptions
Optional
FindOneAndReplaceOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<CollectionType?>
. On success, contains aCollectionType
, representing either the original document or its replacement, depending on selected options; or containingnil
if there was no matching document.If the future fails, the error is likely one of the following:
MongoError.InvalidArgumentError
if any of the provided options are invalid.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this collection’s parent client has already been closed.MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.WriteError
if an error occurs while executing the command.DecodingError
if the replaced document cannot be decoded to aCollectionType
value.EncodingError
ifreplacement
cannot be encoded to aBSONDocument
.
-
Finds a single document and updates it, returning either the original or the updated document.
Declaration
Swift
public func findOneAndUpdate( filter: BSONDocument, update: BSONDocument, options: FindOneAndUpdateOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<CollectionType?>
Parameters
filter
BSONDocument
representing the match criteriaupdate
a
BSONDocument
containing updates to applyoptions
Optional
FindOneAndUpdateOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<CollectionType>
. On success, contains either the original or updated document, depending on selected options, or containsnil
if there was no match.If the future fails, the error is likely one of the following:
MongoError.InvalidArgumentError
if any of the provided options are invalid.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this collection’s parent client has already been closed.MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.WriteError
if an error occurs while executing the command.DecodingError
if the updated document cannot be decoded to aCollectionType
value.
-
Finds a single document and updates it, returning either the original or the updated document.
Declaration
Swift
public func findOneAndUpdate( filter: BSONDocument, pipeline: [BSONDocument], options: FindOneAndUpdateOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<CollectionType?>
Parameters
filter
BSONDocument
representing the match criteriapipeline
an array of
BSONDocument
containing the aggregation pipeline to applyoptions
Optional
FindOneAndUpdateOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<CollectionType>
. On success, contains either the original or updated document, depending on selected options, or containsnil
if there was no match.If the future fails, the error is likely one of the following:
MongoError.InvalidArgumentError
if any of the provided options are invalid.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this collection’s parent client has already been closed.MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.WriteError
if an error occurs while executing the command.DecodingError
if the updated document cannot be decoded to aCollectionType
value.
-
Creates an index over the collection for the provided keys with the provided options.
Declaration
Swift
public func createIndex( _ keys: BSONDocument, indexOptions: IndexOptions? = nil, options: CreateIndexOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<String>
Parameters
keys
a
BSONDocument
specifing the keys for the indexindexOptions
Optional
IndexOptions
to use for the indexoptions
Optional
CreateIndexOptions
to use for the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<String>
. On success, contains the name of the created index.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the write.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the index specification or options.
-
Creates an index over the collection for the provided keys with the provided options.
Declaration
Swift
public func createIndex( _ model: IndexModel, options: CreateIndexOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<String>
Parameters
model
An
IndexModel
representing the keys and options for the indexoptions
Optional
CreateIndexOptions
to use for the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<String>
. On success, contains the name of the created index.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the write.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the index specification or options.
-
Creates multiple indexes in the collection.
Declaration
Swift
public func createIndexes( _ models: [IndexModel], options: CreateIndexOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<[String]>
Parameters
models
An
[IndexModel]
specifying the indexes to createoptions
Optional
CreateIndexOptions
to use for the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<[String]>
. On success, contains the names of the created indexes.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the write.MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.InvalidArgumentError
ifmodels
is empty.MongoError.InvalidArgumentError
if the options passed in form an invalid combination.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the index specifications or options.
-
Drops a single index from the collection by the index name.
Declaration
Swift
public func dropIndex( _ name: String, options: DropIndexOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<Void>
Parameters
name
The name of the index to drop
options
Optional
DropIndexOptions
to use for the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<Void>
that succeeds when the drop is successful.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the command.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this collection’s parent client has already been closed.MongoError.CommandError
if an error occurs that prevents the command from executing.MongoError.InvalidArgumentError
if the options passed in form an invalid combination.EncodingError
if an error occurs while encoding the options.
-
Attempts to drop a single index from the collection given the keys describing it.
Declaration
Swift
public func dropIndex( _ keys: BSONDocument, options: DropIndexOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<Void>
Parameters
keys
a
BSONDocument
containing the keys for the index to dropoptions
Optional
DropIndexOptions
to use for the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<Void>
that succeeds when the drop is successful.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the command.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options.
-
Attempts to drop a single index from the collection given an
IndexModel
describing it.Declaration
Swift
public func dropIndex( _ model: IndexModel, options: DropIndexOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<Void>
Parameters
model
An
IndexModel
describing the index to dropoptions
Optional
DropIndexOptions
to use for the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<Void>
that succeeds when the drop is successful.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the command.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options.
-
Drops all indexes in the collection.
Declaration
Swift
public func dropIndexes( options: DropIndexOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<Void>
Parameters
options
Optional
DropIndexOptions
to use for the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<Void>
that succeeds when the drop is successful.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the command.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options.
-
Retrieves a list of the indexes currently on this collection.
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 listIndexes(session: ClientSession? = nil) -> EventLoopFuture<MongoCursor<IndexModel>>
Parameters
session
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<MongoCursor<IndexModel>>
. On success, contains a cursor over the indexes.If the future fails, the error is likely one of the following:
MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this collection’s parent client has already been closed.
-
Retrieves a list of names of the indexes currently on this collection.
Declaration
Swift
public func listIndexNames(session: ClientSession? = nil) -> EventLoopFuture<[String]>
Parameters
session
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<[String]>
containing the index names.If the future fails, the error is likely one of the following:
MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this collection’s parent client has already been closed.
-
Finds the documents in this collection which match the provided filter.
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 find( _ filter: BSONDocument = [:], options: FindOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<MongoCursor<CollectionType>>
Parameters
filter
A
BSONDocument
that should match the queryoptions
Optional
FindOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<MongoCursor<CollectionType>
. On success, contains a cursor over the resulting documents.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
Finds a single document in this collection that matches the provided filter.
Declaration
Swift
public func findOne( _ filter: BSONDocument = [:], options: FindOneOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<T?>
Parameters
filter
A
BSONDocument
that should match the queryoptions
Optional
FindOneOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<CollectionType?>
. On success, contains the matching document, or nil if there was no match.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 collection’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 collection.
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 performoptions
Optional
AggregateOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<MongoCursor<CollectionType>
. On success, contains a cursor over the resulting documents.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 collection’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 collection. Associates the specified
Codable
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 performoptions
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.InvalidArgumentError
if the options passed are an invalid combination.MongoError.LogicError
if the provided session is inactive.MongoError.LogicError
if this collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
Counts the number of documents in this collection matching the provided filter. Note that an empty filter will force a scan of the entire collection. For a fast count of the total documents in a collection see
estimatedDocumentCount
.Declaration
Swift
public func countDocuments( _ filter: BSONDocument = [:], options: CountDocumentsOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<Int>
Parameters
filter
a
BSONDocument
, the filter that documents must match in order to be countedoptions
Optional
CountDocumentsOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<Int>
. On success, contains the count of the documents that matched the filter.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
Gets an estimate of the count of documents in this collection using collection metadata. This operation cannot be used in a transaction.
Declaration
Swift
public func estimatedDocumentCount(options: EstimatedDocumentCountOptions? = nil) -> EventLoopFuture<Int>
Parameters
options
Optional
EstimatedDocumentCountOptions
to use when executing the commandReturn Value
An
EventLoopFuture<Int>
. On success, contains an estimate of the count of documents in this 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 this collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
Finds the distinct values for a specified field across the collection.
Declaration
Swift
public func distinct( fieldName: String, filter: BSONDocument = [:], options: DistinctOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<[BSON]>
Parameters
fieldName
The field for which the distinct values will be found
filter
a
BSONDocument
representing the filter documents must match in order to be considered for the operationoptions
Optional
DistinctOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<[BSON]>
. On success, contains the distinct values for the specified criteria.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
Encodes the provided value to BSON and inserts it. If the value is missing an identifier, one will be generated for it.
Declaration
Swift
public func insertOne( _ value: CollectionType, options: InsertOneOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<InsertOneResult?>
Parameters
value
A
CollectionType
value to encode and insertoptions
Optional
InsertOneOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<InsertOneResult?>
. On success, contains the result of performing the insert, or containsnil
if the write concern is unacknowledged.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the command.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding theCollectionType
to BSON.
-
Encodes the provided values to BSON and inserts them. If any values are missing identifiers, the driver will generate them.
Declaration
Swift
public func insertMany( _ values: [CollectionType], options: InsertManyOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<InsertManyResult?>
Parameters
values
The
CollectionType
values to insertoptions
optional
InsertManyOptions
to use while executing the operationsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<InsertManyResult?>
. On success, contains the result of performing the inserts, or containsnil
if the write concern is unacknowledged.If the future fails, the error is likely one of the following:
MongoError.BulkWriteError
if an error occurs while performing any of the writes.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding theCollectionType
or options to BSON.
-
Replaces a single document matching the provided filter in this collection.
Declaration
Swift
public func replaceOne( filter: BSONDocument, replacement: CollectionType, options: ReplaceOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<UpdateResult?>
Parameters
filter
A
BSONDocument
representing the match criteriareplacement
The replacement value, a
CollectionType
value to be encoded and insertedoptions
Optional
ReplaceOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<UpdateResult?>
. On success, contains the result of performing the replacement, or containsnil
if the write concern is unacknowledged.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the command.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding theCollectionType
or options to BSON.
-
Updates a single document matching the provided filter in this collection.
Declaration
Swift
public func updateOne( filter: BSONDocument, update: BSONDocument, options: UpdateOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<UpdateResult?>
Parameters
filter
A
BSONDocument
representing the match criteriaupdate
A
BSONDocument
representing the update to be applied to a matching documentoptions
Optional
UpdateOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<UpdateResult?>
. On success, contains the result of performing the update, or containsnil
if the write concern is unacknowledged.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the command.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
Updates a single document matching the provided filter in this collection.
Declaration
Swift
public func updateOne( filter: BSONDocument, pipeline: [BSONDocument], options: UpdateOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<UpdateResult?>
Parameters
filter
A
BSONDocument
representing the match criteriapipeline
An array of
BSONDocument
representing the aggregation pipeline to be applied to a matching documentoptions
Optional
UpdateOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<UpdateResult?>
. On success, contains the result of performing the update, or containsnil
if the write concern is unacknowledged.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the command.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
Updates multiple documents matching the provided filter in this collection.
Declaration
Swift
public func updateMany( filter: BSONDocument, update: BSONDocument, options: UpdateOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<UpdateResult?>
Parameters
filter
A
BSONDocument
representing the match criteriaupdate
A
BSONDocument
representing the update to be applied to matching documentsoptions
Optional
UpdateOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<UpdateResult?>
. On success, contains the result of performing the update, or containsnil
if the write concern is unacknowledged.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the command.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
Updates multiple documents matching the provided filter in this collection.
Declaration
Swift
public func updateMany( filter: BSONDocument, pipeline: [BSONDocument], options: UpdateOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<UpdateResult?>
Parameters
filter
A
BSONDocument
representing the match criteriapipeline
An array of
BSONDocument
representing the aggregation pipeline to be applied to matching documentsoptions
Optional
UpdateOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<UpdateResult?>
. On success, contains the result of performing the update, or containsnil
if the write concern is unacknowledged.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the command.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
Deletes a single matching document from the collection.
Declaration
Swift
public func deleteOne( _ filter: BSONDocument, options: DeleteOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<DeleteResult?>
Parameters
filter
A
BSONDocument
representing the match criteriaoptions
Optional
DeleteOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<DeleteResult?>
. On success, contains the result of performing the deletion, or containsnil
if the write concern is unacknowledged.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the command.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.
-
Deletes all matching documents from the collection.
Declaration
Swift
public func deleteMany( _ filter: BSONDocument, options: DeleteOptions? = nil, session: ClientSession? = nil ) -> EventLoopFuture<DeleteResult?>
Parameters
filter
Document representing the match criteria
options
Optional
DeleteOptions
to use when executing the commandsession
Optional
ClientSession
to use when executing this commandReturn Value
An
EventLoopFuture<DeleteResult?>
. On success, contains the result of performing the deletions, or containsnil
if the write concern is unacknowledged.If the future fails, the error is likely one of the following:
MongoError.WriteError
if an error occurs while performing the command.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 collection’s parent client has already been closed.EncodingError
if an error occurs while encoding the options to BSON.