MongoCollection
public struct MongoCollection<T> where T : Decodable, T : Encodable
A MongoDB collection.
-
Encoder used by this collection for BSON conversions. (e.g. converting
CollectionTypes, indexes, and options to documents).Declaration
Swift
public var encoder: BSONEncoder { get } -
Decoder used by this collection for BSON conversions (e.g. converting documents to
CollectionTypes).Declaration
Swift
public var decoder: BSONDecoder { get } -
A
Codabletype associated with thisMongoCollectioninstance. This allowsCollectionTypevalues to be directly inserted into and retrieved from the collection, by encoding/decoding them using theBSONEncoderandBSONDecoder. 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
MongoCollectioninstance. 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 namespace for this collection.
Declaration
Swift
public var namespace: MongoNamespace { get } -
The name of this collection.
Declaration
Swift
public var name: String { get } -
The
ReadConcernset on this collection, ornilif one is not set.Declaration
Swift
public var readConcern: ReadConcern? { get } -
The
ReadPreferenceset on this collection.Declaration
Swift
public var readPreference: ReadPreference { get } -
The
WriteConcernset on this collection, or nil if one is not set.Declaration
Swift
public var writeConcern: WriteConcern? { get } -
Drops this collection from its parent database.
Declaration
Swift
public func drop(options: DropCollectionOptions? = nil, session: ClientSession? = nil) throws -
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
dropTargetoption 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 ) throws -> MongoCollectionParameters
toA
String, the new name for the collectionoptionsOptional
RenameCollectionOptionsto use for the collectionsessionOptional
ClientSessionto use when executing this commandReturn Value
A copy of the target
MongoCollectionwith the new name.Throws:
MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.MongoError.LogicErrorif this collection’s parent client has already been closed.EncodingErrorif an error occurs while encoding the options to BSON.
-
Starts a
ChangeStreamon a collection. TheCollectionTypewill be associated with thefullDocumentfield inChangeStreamEvents emitted by the returnedChangeStream. The server will return an error if this method is called on a system collection.Throws
Throws:
MongoError.CommandErrorif an error occurs on the server while creating the change stream.MongoError.InvalidArgumentErrorif the options passed formed an invalid combination.MongoError.InvalidArgumentErrorif the_idfield is projected out of the change stream documents by the pipeline.
Declaration
Swift
public func watch( _ pipeline: [BSONDocument] = [], options: ChangeStreamOptions? = nil, session: ClientSession? = nil ) throws -> ChangeStream<ChangeStreamEvent<CollectionType>>Parameters
pipelineAn array of aggregation pipeline stages to apply to the events returned by the change stream.
optionsAn optional
ChangeStreamOptionsto use when constructing the change stream.sessionAn optional
ClientSessionto use with this change stream.Return Value
A
ChangeStreamon a specific collection. -
Starts a
ChangeStreamon a collection. Associates the specifiedCodabletypeTwith thefullDocumentfield in theChangeStreamEvents emitted by the returnedChangeStream. The server will return an error if this method is called on a system collection.Throws
Throws:
MongoError.CommandErrorif an error occurs on the server while creating the change stream.MongoError.InvalidArgumentErrorif the options passed formed an invalid combination.MongoError.InvalidArgumentErrorif the_idfield is projected out of the change stream documents by the pipeline.
Declaration
Swift
public func watch<FullDocType: Codable>( _ pipeline: [BSONDocument] = [], options: ChangeStreamOptions? = nil, session: ClientSession? = nil, withFullDocumentType _: FullDocType.Type ) throws -> ChangeStream<ChangeStreamEvent<FullDocType>>Parameters
pipelineAn array of aggregation pipeline stages to apply to the events returned by the change stream.
optionsAn optional
ChangeStreamOptionsto use when constructing the change stream.sessionAn optional
ClientSessionto use with this change stream.withFullDocumentTypeThe type that the
fullDocumentfield of the emittedChangeStreamEvents will be decoded to.Return Value
A
ChangeStreamon a specific collection. -
Starts a
ChangeStreamon a collection. Associates the specifiedCodabletypeTwith the returnedChangeStream. The server will return an error if this method is called on a system collection.Throws
Throws:
MongoError.CommandErrorif an error occurs on the server while creating the change stream.MongoError.InvalidArgumentErrorif the options passed formed an invalid combination.MongoError.InvalidArgumentErrorif the_idfield is projected out of the change stream documents by the pipeline.
Declaration
Swift
public func watch<EventType: Codable>( _ pipeline: [BSONDocument] = [], options: ChangeStreamOptions? = nil, session: ClientSession? = nil, withEventType _: EventType.Type ) throws -> ChangeStream<EventType>Parameters
pipelineAn array of aggregation pipeline stages to apply to the events returned by the change stream.
optionsAn optional
ChangeStreamOptionsto use when constructing the change stream.sessionAn optional
ClientSessionto use with this change stream.withEventTypeThe type that the entire change stream response will be decoded to and that will be returned when iterating through the change stream.
Return Value
A
ChangeStreamon a specific collection. -
Finds a single document and deletes it, returning the original.
Throws
Throws:
MongoError.InvalidArgumentErrorif any of the provided options are invalid.MongoError.LogicErrorif the provided session is inactive.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.WriteErrorif an error occurs while executing the command.DecodingErrorif the deleted document cannot be decoded to aCollectionTypevalue.
Declaration
Swift
@discardableResult public func findOneAndDelete( _ filter: BSONDocument, options: FindOneAndDeleteOptions? = nil, session: ClientSession? = nil ) throws -> CollectionType?Parameters
filterDocumentrepresenting the match criteriaoptionsOptional
FindOneAndDeleteOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
The deleted document, represented as a
CollectionType, ornilif no document was deleted. -
Finds a single document and replaces it, returning either the original or the replaced document.
Throws
Throws:
MongoError.InvalidArgumentErrorif any of the provided options are invalid.MongoError.LogicErrorif the provided session is inactive.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.WriteErrorif an error occurs while executing the command.DecodingErrorif the replaced document cannot be decoded to aCollectionTypevalue.EncodingErrorifreplacementcannot be encoded to aDocument.
Declaration
Swift
@discardableResult public func findOneAndReplace( filter: BSONDocument, replacement: CollectionType, options: FindOneAndReplaceOptions? = nil, session: ClientSession? = nil ) throws -> CollectionType?Parameters
filterDocumentrepresenting the match criteriareplacementa
CollectionTypeto replace the found documentoptionsOptional
FindOneAndReplaceOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
A
CollectionType, representing either the original document or its replacement, depending on selected options, ornilif there was no match. -
Finds a single document and updates it, returning either the original or the updated document.
Throws
Throws:
MongoError.InvalidArgumentErrorif any of the provided options are invalid.MongoError.LogicErrorif the provided session is inactive.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.WriteErrorif an error occurs while executing the command.DecodingErrorif the updated document cannot be decoded to aCollectionTypevalue.
Declaration
Swift
@discardableResult public func findOneAndUpdate( filter: BSONDocument, update: BSONDocument, options: FindOneAndUpdateOptions? = nil, session: ClientSession? = nil ) throws -> CollectionType?Parameters
filterDocumentrepresenting the match criteriaupdatea
Documentcontaining updates to applyoptionsOptional
FindOneAndUpdateOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
A
CollectionTyperepresenting either the original or updated document, depending on selected options, ornilif there was no match. -
Finds a single document and updates it, returning either the original or the updated document.
Throws
Throws:
MongoError.InvalidArgumentErrorif any of the provided options are invalid.MongoError.LogicErrorif the provided session is inactive.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.WriteErrorif an error occurs while executing the command.DecodingErrorif the updated document cannot be decoded to aCollectionTypevalue.
Declaration
Swift
@discardableResult public func findOneAndUpdate( filter: BSONDocument, pipeline: [BSONDocument], options: FindOneAndUpdateOptions? = nil, session: ClientSession? = nil ) throws -> CollectionType?Parameters
filterDocumentrepresenting the match criteriapipelinean array of
Documentcontaining the aggregation pipeline to applyoptionsOptional
FindOneAndUpdateOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
A
CollectionTyperepresenting either the original or updated document, depending on selected options, ornilif there was no match. -
Creates an index over the collection for the provided keys with the provided options.
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the write.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the index specification or options.
Declaration
Swift
@discardableResult public func createIndex( _ keys: BSONDocument, indexOptions: IndexOptions? = nil, options: CreateIndexOptions? = nil, session: ClientSession? = nil ) throws -> StringParameters
keysa
Documentspecifing the keys for the indexindexOptionsOptional
IndexOptionsto use for the indexoptionsOptional
CreateIndexOptionsto use for the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
The name of the created index.
-
Creates an index over the collection for the provided keys with the provided options.
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the write.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the index specification or options.
Declaration
Swift
@discardableResult public func createIndex( _ model: IndexModel, options: CreateIndexOptions? = nil, session: ClientSession? = nil ) throws -> StringParameters
modelAn
IndexModelrepresenting the keys and options for the indexoptionsOptional
CreateIndexOptionsto use for the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
The name of the created index.
-
Creates multiple indexes in the collection.
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the write.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the index specifications or options.
Declaration
Swift
@discardableResult public func createIndexes( _ models: [IndexModel], options: CreateIndexOptions? = nil, session: ClientSession? = nil ) throws -> [String]Parameters
modelsAn
[IndexModel]specifying the indexes to createoptionsOptional
CreateIndexOptionsto use for the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
An
[String]containing the names of all the indexes that were created. -
Drops a single index from the collection by the index name.
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the command.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.EncodingErrorif an error occurs while encoding the options.
Declaration
Swift
public func dropIndex( _ name: String, options: DropIndexOptions? = nil, session: ClientSession? = nil ) throwsParameters
nameThe name of the index to drop
optionsOptional
DropIndexOptionsto use for the commandsessionOptional
ClientSessionto use when executing this command -
Attempts to drop a single index from the collection given the keys describing it.
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the command.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options.
Declaration
Swift
public func dropIndex( _ keys: BSONDocument, options: DropIndexOptions? = nil, session: ClientSession? = nil ) throwsParameters
keysa
Documentcontaining the keys for the index to dropoptionsOptional
DropIndexOptionsto use for the commandsessionOptional
ClientSessionto use when executing this command -
Attempts to drop a single index from the collection given an
IndexModeldescribing it.Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the command.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options.
Declaration
Swift
public func dropIndex( _ model: IndexModel, options: DropIndexOptions? = nil, session: ClientSession? = nil ) throwsParameters
modelAn
IndexModeldescribing the index to dropoptionsOptional
DropIndexOptionsto use for the commandsessionOptional
ClientSessionto use when executing this command -
Drops all indexes in the collection.
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the command.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options.
Declaration
Swift
public func dropIndexes( options: DropIndexOptions? = nil, session: ClientSession? = nil ) throwsParameters
optionsOptional
DropIndexOptionsto use for the commandsessionOptional
ClientSessionto use when executing this command -
Retrieves a list of the indexes currently on this collection.
Throws
MongoError.LogicErrorif the provided session is inactive.Declaration
Swift
public func listIndexes(session: ClientSession? = nil) throws -> MongoCursor<IndexModel>Parameters
sessionOptional
ClientSessionto use when executing this commandReturn Value
A
MongoCursorover theIndexModels. -
Retrieves a list of names of the indexes currently on this collection.
Throws
MongoError.LogicErrorif the provided session is inactive.Declaration
Swift
public func listIndexNames(session: ClientSession? = nil) throws -> [String]Parameters
sessionOptional
ClientSessionto use when executing this commandReturn Value
A
MongoCursorover the index names. -
Finds the documents in this collection which match the provided filter.
Throws
Throws:
MongoError.InvalidArgumentErrorif the options passed are an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options to BSON.
Declaration
Swift
public func find( _ filter: BSONDocument = [:], options: FindOptions? = nil, session: ClientSession? = nil ) throws -> MongoCursor<CollectionType>Parameters
filterA
Documentthat should match the queryoptionsOptional
FindOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
A
MongoCursorover the resultingDocuments -
Finds a single document in this collection that matches the provided filter.
Throws
Throws:
MongoError.InvalidArgumentErrorif the options passed are an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options to BSON.
Declaration
Swift
public func findOne( _ filter: BSONDocument = [:], options: FindOneOptions? = nil, session: ClientSession? = nil ) throws -> T?Parameters
filterA
Documentthat should match the queryoptionsOptional
FindOneOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
the resulting
Document, or nil if there is no match -
Runs an aggregation framework pipeline against this collection.
Throws
Throws:
MongoError.InvalidArgumentErrorif the options passed are an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options to BSON.
Declaration
Swift
public func aggregate( _ pipeline: [BSONDocument], options: AggregateOptions? = nil, session: ClientSession? = nil ) throws -> MongoCursor<BSONDocument>Parameters
pipelinean
[BSONDocument]containing the pipeline of aggregation operations to performoptionsOptional
AggregateOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
A
MongoCursorover the resultingDocuments -
Runs an aggregation framework pipeline against this collection. Associates the specified
CodabletypeOutputTypewith the returnedMongoCursorThrows
Throws:
MongoError.InvalidArgumentErrorif the options passed are an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif 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 ) throws -> MongoCursor<OutputType>Parameters
pipelinean
[BSONDocument]containing the pipeline of aggregation operations to performoptionsOptional
AggregateOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandwithOutputTypethe type that each resulting document of the output of the aggregation operation will be decoded to
Return Value
A
MongoCursorover the resultingOutputTypes -
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 ) throws -> IntParameters
filtera
Document, the filter that documents must match in order to be countedoptionsOptional
CountDocumentsOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
The count of the documents that matched the filter
-
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) throws -> IntParameters
optionsOptional
EstimatedDocumentCountOptionsto use when executing the commandReturn Value
an estimate of the count of documents in this collection
-
Finds the distinct values for a specified field across the collection.
Throws
Throws:
MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options to BSON.
Declaration
Swift
public func distinct( fieldName: String, filter: BSONDocument = [:], options: DistinctOptions? = nil, session: ClientSession? = nil ) throws -> [BSON]Parameters
fieldNameThe field for which the distinct values will be found
filtera
Documentrepresenting the filter documents must match in order to be considered for the operationoptionsOptional
DistinctOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
A
[BSONValue]containing the distinct values for the specified criteria -
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.WriteErrorif an error occurs while performing the command.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding theCollectionTypeto BSON.
Declaration
Swift
@discardableResult public func insertOne( _ value: CollectionType, options: InsertOneOptions? = nil, session: ClientSession? = nil ) throws -> InsertOneResult?Parameters
valueA
CollectionTypevalue to encode and insertoptionsOptional
InsertOneOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
The optional result of attempting to perform the insert. If the
WriteConcernis unacknowledged,nilis returned. -
Encodes the provided values to BSON and inserts them. If any values are missing identifiers, the driver will generate them.
Throws
Throws:
MongoError.BulkWriteErrorif an error occurs while performing any of the writes.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding theCollectionTypeor options to BSON.
Declaration
Swift
@discardableResult public func insertMany( _ values: [CollectionType], options: InsertManyOptions? = nil, session: ClientSession? = nil ) throws -> InsertManyResult?Parameters
valuesThe
CollectionTypevalues to insertoptionsoptional
InsertManyOptionsto use while executing the operationsessionOptional
ClientSessionto use when executing this commandReturn Value
an
InsertManyResult, ornilif the write concern is unacknowledged. -
Replaces a single document matching the provided filter in this collection.
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the command.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding theCollectionTypeor options to BSON.
Declaration
Swift
@discardableResult public func replaceOne( filter: BSONDocument, replacement: CollectionType, options: ReplaceOptions? = nil, session: ClientSession? = nil ) throws -> UpdateResult?Parameters
filterA
Documentrepresenting the match criteriareplacementThe replacement value, a
CollectionTypevalue to be encoded and insertedoptionsOptional
ReplaceOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
The optional result of attempting to replace a document. If the
WriteConcernis unacknowledged,nilis returned. -
Updates a single document matching the provided filter in this collection.
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the command.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options to BSON.
Declaration
Swift
@discardableResult public func updateOne( filter: BSONDocument, update: BSONDocument, options: UpdateOptions? = nil, session: ClientSession? = nil ) throws -> UpdateResult?Parameters
filterA
Documentrepresenting the match criteriaupdateA
Documentrepresenting the update to be applied to a matching documentoptionsOptional
UpdateOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
The optional result of attempting to update a document. If the
WriteConcernis unacknowledged,nilis returned. -
Updates a single document matching the provided filter in this collection.
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the command.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options to BSON.
Declaration
Swift
@discardableResult public func updateOne( filter: BSONDocument, pipeline: [BSONDocument], options: UpdateOptions? = nil, session: ClientSession? = nil ) throws -> UpdateResult?Parameters
filterA
Documentrepresenting the match criteriapipelineAn array of
Documentrepresenting the aggregation pipeline to be applied to a matching documentoptionsOptional
UpdateOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
The optional result of attempting to update a document. If the
WriteConcernis unacknowledged,nilis returned. -
Updates multiple documents matching the provided filter in this collection.
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the command.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options to BSON.
Declaration
Swift
@discardableResult public func updateMany( filter: BSONDocument, update: BSONDocument, options: UpdateOptions? = nil, session: ClientSession? = nil ) throws -> UpdateResult?Parameters
filterA
Documentrepresenting the match criteriaupdateA
Documentrepresenting the update to be applied to matching documentsoptionsOptional
UpdateOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
The optional result of attempting to update multiple documents. If the write concern is unacknowledged, nil is returned
-
Updates multiple documents matching the provided filter in this collection.
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the command.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options to BSON.
Declaration
Swift
@discardableResult public func updateMany( filter: BSONDocument, pipeline: [BSONDocument], options: UpdateOptions? = nil, session: ClientSession? = nil ) throws -> UpdateResult?Parameters
filterA
Documentrepresenting the match criteriapipelineAn array of
Documentrepresenting the aggregation pipeline to be applied to matching documentsoptionsOptional
UpdateOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
The optional result of attempting to update multiple documents. If the write concern is unacknowledged, nil is returned
-
Deletes a single matching document from the collection.
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the command.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options to BSON.
Declaration
Swift
@discardableResult public func deleteOne( _ filter: BSONDocument, options: DeleteOptions? = nil, session: ClientSession? = nil ) throws -> DeleteResult?Parameters
filterA
Documentrepresenting the match criteriaoptionsOptional
DeleteOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
The optional result of performing the deletion. If the
WriteConcernis unacknowledged,nilis returned. -
Deletes multiple documents
Throws
Throws:
MongoError.WriteErrorif an error occurs while performing the command.MongoError.CommandErrorif an error occurs that prevents the command from executing.MongoError.InvalidArgumentErrorif the options passed in form an invalid combination.MongoError.LogicErrorif the provided session is inactive.EncodingErrorif an error occurs while encoding the options to BSON.
Declaration
Swift
@discardableResult public func deleteMany( _ filter: BSONDocument, options: DeleteOptions? = nil, session: ClientSession? = nil ) throws -> DeleteResult?Parameters
filterDocument representing the match criteria
optionsOptional
DeleteOptionsto use when executing the commandsessionOptional
ClientSessionto use when executing this commandReturn Value
The optional result of performing the deletion. If the
WriteConcernis unacknowledged,nilis returned. -
Execute multiple write operations.
Throws
Throws:
MongoError.InvalidArgumentErrorifrequestsis empty.MongoError.LogicErrorif the provided session is inactive.MongoError.BulkWriteErrorif any error occurs while performing the writes. This includes errors that would typically be thrown asRuntimeErrors orMongoError.CommandErrors elsewhere.EncodingErrorif an error occurs while encoding theCollectionTypeor the options to BSON.
Declaration
Swift
@discardableResult public func bulkWrite( _ requests: [WriteModel<T>], options: BulkWriteOptions? = nil, session: ClientSession? = nil ) throws -> BulkWriteResult?Parameters
requestsa
[WriteModel]containing the writes to perform.optionsoptional
BulkWriteOptionsto use while executing the operation.sessionOptional
ClientSessionto use when executing this commandReturn Value
a
BulkWriteResult, ornilif the write concern is unacknowledged.
View on GitHub
Install in Dash
MongoCollection Structure Reference