MongoCollection
extension MongoCollection
-
Opens a MongoDB change stream against the collection to watch for changes. The resulting stream will be notified of all events on this collection that the active user is authorized to see based on the configured MongoDB rules.
Declaration
Swift
public func watch(delegate: ChangeEventDelegate, queue: DispatchQueue = .main) -> ChangeStreamParameters
delegateThe delegate that will react to events and errors from the resulting change stream.
queueDispatches streaming events to an optional queue, if no queue is provided the main queue is used
Return Value
A ChangeStream which will manage the streaming events.
-
Opens a MongoDB change stream against the collection to watch for changes. The provided BSON document will be used as a match expression filter on the change events coming from the stream.
See https://docs.mongodb.com/manual/reference/operator/aggregation/match/ for documentation around how to define a match filter.
Defining the match expression to filter ChangeEvents is similar to defining the match expression for triggers: https://docs.mongodb.com/realm/triggers/database-triggers/
Declaration
Swift
public func watch(matchFilter: Document, delegate: ChangeEventDelegate, queue: DispatchQueue = .main) -> ChangeStreamParameters
matchFilterThe $match filter to apply to incoming change events
delegateThe delegate that will react to events and errors from the resulting change stream.
queueDispatches streaming events to an optional queue, if no queue is provided the main queue is used
Return Value
A ChangeStream which will manage the streaming events.
-
Opens a MongoDB change stream against the collection to watch for changes made to specific documents. The documents to watch must be explicitly specified by their _id.
Declaration
Swift
public func watch(filterIds: [ObjectId], delegate: ChangeEventDelegate, queue: DispatchQueue = .main) -> ChangeStreamParameters
filterIdsThe list of _ids in the collection to watch.
delegateThe delegate that will react to events and errors from the resulting change stream.
queueDispatches streaming events to an optional queue, if no queue is provided the main queue is used
Return Value
A ChangeStream which will manage the streaming events.
-
Encodes the provided value to BSON and inserts it. If the value is missing an identifier, one will be generated for it.
Declaration
Swift
@preconcurrency public func insertOne(_ document: Document, _ completion: @escaping MongoInsertBlock)Parameters
documentdocument A
Documentvalue to insert.completionThe result of attempting to perform the insert. An Id will be returned for the inserted object on sucess
-
Encodes the provided values to BSON and inserts them. If any values are missing identifiers, they will be generated.
Declaration
Swift
@preconcurrency public func insertMany(_ documents: [Document], _ completion: @escaping MongoInsertManyBlock)Parameters
documentsThe
Documentvalues in a bson array to insert.completionThe result of the insert, returns an array inserted document ids in order.
-
Finds the documents in this collection which match the provided filter.
Declaration
Swift
@preconcurrency public func find(filter: Document, options: FindOptions = FindOptions(), _ completion: @escaping MongoFindBlock)Parameters
filterA
Documentas bson that should match the query.optionsFindOptionsto use when executing the command.completionThe resulting bson array of documents or error if one occurs
-
Returns one document from a collection or view which matches the provided filter. If multiple documents satisfy the query, this method returns the first document according to the query’s sort order or natural order.
Declaration
Swift
@preconcurrency public func findOneDocument(filter: Document, options: FindOptions = FindOptions(), _ completion: @escaping MongoFindOneBlock)Parameters
filterA
Documentas bson that should match the query.optionsFindOptionsto use when executing the command.completionThe resulting bson or error if one occurs
-
Runs an aggregation framework pipeline against this collection.
Declaration
Swift
@preconcurrency public func aggregate(pipeline: [Document], _ completion: @escaping MongoFindBlock)Parameters
pipelineA bson array made up of
Documentscontaining the pipeline of aggregation operations to perform.completionThe resulting bson array of documents or error if one occurs
-
Counts the number of documents in this collection matching the provided filter.
Declaration
Swift
@preconcurrency public func count(filter: Document, limit: Int? = nil, _ completion: @escaping MongoCountBlock)Parameters
filterA
Documentas bson that should match the query.limitThe max amount of documents to count
completionReturns the count of the documents that matched the filter.
-
Deletes a single matching document from the collection.
Declaration
Swift
@preconcurrency public func deleteOneDocument(filter: Document, _ completion: @escaping MongoCountBlock)Parameters
filterA
Documentas bson that should match the query.completionThe result of performing the deletion. Returns the count of deleted objects
-
Deletes multiple documents
Declaration
Swift
@preconcurrency public func deleteManyDocuments(filter: Document, _ completion: @escaping MongoCountBlock)Parameters
filterDocument representing the match criteria
completionThe result of performing the deletion. Returns the count of the deletion
-
Updates a single document matching the provided filter in this collection.
Declaration
Swift
@preconcurrency public func updateOneDocument(filter: Document, update: Document, upsert: Bool = false, _ completion: @escaping MongoUpdateBlock) -
Updates multiple documents matching the provided filter in this collection.
Declaration
Swift
@preconcurrency public func updateManyDocuments(filter: Document, update: Document, upsert: Bool = false, _ completion: @escaping MongoUpdateBlock) -
Updates a single document in a collection based on a query filter and returns the document in either its pre-update or post-update form. Unlike
updateOneDocument, this action allows you to atomically find, update, and return a document with the same command. This avoids the risk of other update operations changing the document between separate find and update operations.Declaration
Swift
@preconcurrency public func findOneAndUpdate(filter: Document, update: Document, options: FindOneAndModifyOptions = .init(), _ completion: @escaping MongoFindOneBlock) -
Overwrites a single document in a collection based on a query filter and returns the document in either its pre-replacement or post-replacement form. Unlike
updateOneDocument, this action allows you to atomically find, replace, and return a document with the same command. This avoids the risk of other update operations changing the document between separate find and update operations.Declaration
Swift
@preconcurrency public func findOneAndReplace(filter: Document, replacement: Document, options: FindOneAndModifyOptions = .init(), _ completion: @escaping MongoFindOneBlock)Parameters
filterA
Documentthat should match the query.replacementA
Documentdescribing the replacement.optionsFindOneAndModifyOptionsto use when executing the command.completionThe result of the attempt to replace a document.
-
Removes a single document from a collection based on a query filter and returns a document with the same form as the document immediately before it was deleted. Unlike
deleteOneDocument, this action allows you to atomically find and delete a document with the same command. This avoids the risk of other update operations changing the document between separate find and delete operations.Declaration
Swift
@preconcurrency public func findOneAndDelete(filter: Document, options: FindOneAndModifyOptions = .init(), _ completion: @escaping MongoFindOneBlock)Parameters
filterA
Documentthat should match the query.optionsFindOneAndModifyOptionsto use when executing the command.completionThe result of the attempt to delete a document.
-
insertOne(_:Asynchronous) Encodes the provided value to BSON and inserts it. If the value is missing an identifier, one will be generated for it.
Parameters
documentA
Documentvalue to insert.Return Value
The object id of the inserted document.
-
insertMany(_:Asynchronous) Encodes the provided values to BSON and inserts them. If any values are missing identifiers, they will be generated.
Parameters
documentsThe
Documentvalues in a bson array to insert.Return Value
The object ids of inserted documents.
-
Finds the documents in this collection which match the provided filter.
-
Returns one document from a collection or view which matches the provided filter. If multiple documents satisfy the query, this method returns the first document according to the query’s sort order or natural order.
-
find(filter:Asynchronousoptions: ) Finds the documents in this collection which match the provided filter.
Declaration
Swift
public func find(filter: Document, options: FindOptions? = nil) async throws -> [Document]Parameters
filterA
Documentas bson that should match the query.optionsFindOptionsto use when executing the command.Return Value
Array of
Documentfiltered. -
findOneDocument(filter:Asynchronousoptions: ) Returns one document from a collection or view which matches the provided filter. If multiple documents satisfy the query, this method returns the first document according to the query’s sort order or natural order.
Declaration
Swift
public func findOneDocument(filter: Document, options: FindOptions? = nil) async throws -> Document?Parameters
filterA
Documentas bson that should match the query.optionsFindOptionsto use when executing the command.Return Value
Documentfiltered. -
aggregate(pipeline:Asynchronous) Runs an aggregation framework pipeline against this collection.
Parameters
pipelineA bson array made up of
Documentscontaining the pipeline of aggregation operations to perform.Return Value
An array of
Documentresult of the aggregation operation. -
count(filter:Asynchronouslimit: ) Counts the number of documents in this collection matching the provided filter.
Declaration
Swift
public func count(filter: Document, limit: Int? = nil) async throws -> IntParameters
filterA
Documentas bson that should match the query.limitThe max amount of documents to count
Return Value
Count of the documents that matched the filter.
-
deleteOneDocument(filter:Asynchronous) -
deleteManyDocuments(filter:Asynchronous) Deletes multiple documents
Declaration
Swift
public func deleteManyDocuments(filter: Document) async throws -> IntParameters
filterDocument representing the match criteria
Return Value
Intcount of deleted documents. -
updateOneDocument(filter:Asynchronousupdate: upsert: ) Updates a single document matching the provided filter in this collection.
Declaration
Swift
public func updateOneDocument(filter: Document, update: Document, upsert: Bool? = nil) async throws -> UpdateResultParameters
filterA bson
Documentrepresenting the match criteria.updateA bson
Documentrepresenting the update to be applied to a matching document.upsertWhen true, creates a new document if no document matches the query.
Return Value
UpdateResultresult of theupdateOneoperation. -
updateManyDocuments(filter:Asynchronousupdate: upsert: ) Updates multiple documents matching the provided filter in this collection.
Declaration
Swift
public func updateManyDocuments(filter: Document, update: Document, upsert: Bool? = nil) async throws -> UpdateResultParameters
filterA bson
Documentrepresenting the match criteria.updateA bson
Documentrepresenting the update to be applied to a matching document.upsertWhen true, creates a new document if no document matches the query.
Return Value
UpdateResultresult of theupdateManyoperation. -
Updates a single document in a collection based on a query filter and returns the document in either its pre-update or post-update form. Unlike
updateOneDocument, this action allows you to atomically find, update, and return a document with the same command. This avoids the risk of other update operations changing the document between separate find and update operations. -
Overwrites a single document in a collection based on a query filter and returns the document in either its pre-replacement or post-replacement form. Unlike
updateOneDocument, this action allows you to atomically find, replace, and return a document with the same command. This avoids the risk of other update operations changing the document between separate find and update operations. -
Removes a single document from a collection based on a query filter and returns a document with the same form as the document immediately before it was deleted. Unlike
deleteOneDocument, this action allows you to atomically find and delete a document with the same command. This avoids the risk of other update operations changing the document between separate find and delete operations. -
findOneAndUpdate(filter:Asynchronousupdate: options: ) Updates a single document in a collection based on a query filter and returns the document in either its pre-update or post-update form. Unlike
updateOneDocument, this action allows you to atomically find, update, and return a document with the same command. This avoids the risk of other update operations changing the document between separate find and update operations.Declaration
Swift
public func findOneAndUpdate(filter: Document, update: Document, options: FindOneAndModifyOptions? = nil) async throws -> Document?Parameters
filterA bson
Documentrepresenting the match criteria.updateA bson
Documentrepresenting the update to be applied to a matching document.optionsRemoteFindOneAndModifyOptionsto use when executing the command.Return Value
Documentresult of the attempt to update a document ornilif document wasn’t found. -
findOneAndReplace(filter:Asynchronousreplacement: options: ) Overwrites a single document in a collection based on a query filter and returns the document in either its pre-replacement or post-replacement form. Unlike
updateOneDocument, this action allows you to atomically find, replace, and return a document with the same command. This avoids the risk of other update operations changing the document between separate find and update operations.Declaration
Swift
public func findOneAndReplace(filter: Document, replacement: Document, options: FindOneAndModifyOptions? = nil) async throws -> Document?Parameters
filterA
Documentthat should match the query.replacementA
Documentdescribing the replacement.optionsFindOneAndModifyOptionsto use when executing the command.Return Value
Documentresult of the attempt to reaplce a document ornilif document wasn’t found. -
findOneAndDelete(filter:Asynchronousoptions: ) Removes a single document from a collection based on a query filter and returns a document with the same form as the document immediately before it was deleted. Unlike
deleteOneDocument, this action allows you to atomically find and delete a document with the same command. This avoids the risk of other update operations changing the document between separate find and delete operations.Declaration
Swift
public func findOneAndDelete(filter: Document, options: FindOneAndModifyOptions? = nil) async throws -> Document?Parameters
filterA
Documentthat should match the query.optionsFindOneAndModifyOptionsto use when executing the command.Return Value
Documentresult of the attempt to delete a document ornilif document wasn’t found. -
Creates a publisher that emits a AnyBSON change event each time the MongoDB collection changes.
Declaration
Swift
public func watch() -> Publishers.WatchPublisherReturn Value
A publisher that emits the AnyBSON change event each time the collection changes.
-
Creates a publisher that emits a AnyBSON change event each time the MongoDB collection changes.
Declaration
Swift
public func watch(filterIds: [ObjectId]) -> Publishers.WatchPublisherParameters
filterIdsThe list of _ids in the collection to watch.
Return Value
A publisher that emits the AnyBSON change event each time the collection changes.
-
Creates a publisher that emits a AnyBSON change event each time the MongoDB collection changes.
Declaration
Swift
public func watch(matchFilter: Document) -> Publishers.WatchPublisherParameters
matchFilterThe $match filter to apply to incoming change events.
Return Value
A publisher that emits the AnyBSON change event each time the collection changes.
-
Encodes the provided value to BSON and inserts it. If the value is missing an identifier, one will be generated for it.
Parameters
documentA
Documentvalue to insert.Return Value
A publisher that eventually return the object id of the inserted document or
Error. -
Encodes the provided values to BSON and inserts them. If any values are missing identifiers, they will be generated.
Parameters
documentsThe
Documentvalues in a bson array to insert.Return Value
A publisher that eventually return the object ids of inserted documents or
Error. -
Finds the documents in this collection which match the provided filter.
Declaration
Swift
func find(filter: Document, options: FindOptions) -> Future<[Document], Error>Parameters
filterA
Documentas bson that should match the query.optionsFindOptionsto use when executing the command.Return Value
A publisher that eventually return
[ObjectId]of documents orError. -
Finds the documents in this collection which match the provided filter.
Parameters
filterA
Documentas bson that should match the query.Return Value
A publisher that eventually return
[ObjectId]of documents orError. -
Returns one document from a collection or view which matches the provided filter. If multiple documents satisfy the query, this method returns the first document according to the query’s sort order or natural order.
Declaration
Swift
func findOneDocument(filter: Document, options: FindOptions) -> Future<Document?, Error>Parameters
filterA
Documentas bson that should match the query.optionsFindOptionsto use when executing the command.Return Value
A publisher that eventually return
DocumentorError. -
Returns one document from a collection or view which matches the provided filter. If multiple documents satisfy the query, this method returns the first document according to the query’s sort order or natural order.
Parameters
filterA
Documentas bson that should match the query.Return Value
A publisher that eventually return
Documentornilif document wasn’t found orError. -
Runs an aggregation framework pipeline against this collection.
Parameters
pipelineA bson array made up of
Documentscontaining the pipeline of aggregation operations to perform.Return Value
A publisher that eventually return
DocumentorError. -
Counts the number of documents in this collection matching the provided filter.
Declaration
Swift
func count(filter: Document, limit: Int) -> Future<Int, Error>Parameters
filterA
Documentas bson that should match the query.limitThe max amount of documents to count
Return Value
A publisher that eventually return
Intcount of documents orError. -
Deletes multiple documents
Declaration
Swift
func deleteManyDocuments(filter: Document) -> Future<Int, Error>Parameters
filterDocument representing the match criteria
Return Value
A publisher that eventually return
Intcount of deleted documents orError. -
Updates a single document matching the provided filter in this collection.
Declaration
Swift
func updateOneDocument(filter: Document, update: Document, upsert: Bool) -> Future<UpdateResult, Error>Parameters
filterA bson
Documentrepresenting the match criteria.updateA bson
Documentrepresenting the update to be applied to a matching document.upsertWhen true, creates a new document if no document matches the query.
Return Value
A publisher that eventually return
UpdateResultorError. -
Updates a single document matching the provided filter in this collection.
Declaration
Swift
func updateOneDocument(filter: Document, update: Document) -> Future<UpdateResult, Error>Parameters
filterA bson
Documentrepresenting the match criteria.updateA bson
Documentrepresenting the update to be applied to a matching document.Return Value
A publisher that eventually return
UpdateResultorError. -
Updates multiple documents matching the provided filter in this collection.
Declaration
Swift
func updateManyDocuments(filter: Document, update: Document, upsert: Bool) -> Future<UpdateResult, Error>Parameters
filterA bson
Documentrepresenting the match criteria.updateA bson
Documentrepresenting the update to be applied to a matching document.upsertWhen true, creates a new document if no document matches the query.
Return Value
A publisher that eventually return
UpdateResultorError. -
Updates multiple documents matching the provided filter in this collection.
Declaration
Swift
func updateManyDocuments(filter: Document, update: Document) -> Future<UpdateResult, Error>Parameters
filterA bson
Documentrepresenting the match criteria.updateA bson
Documentrepresenting the update to be applied to a matching document.Return Value
A publisher that eventually return
UpdateResultorError. -
Updates a single document in a collection based on a query filter and returns the document in either its pre-update or post-update form. Unlike
updateOneDocument, this action allows you to atomically find, update, and return a document with the same command. This avoids the risk of other update operations changing the document between separate find and update operations.Declaration
Swift
func findOneAndUpdate(filter: Document, update: Document, options: FindOneAndModifyOptions) -> Future<Document?, Error>Parameters
filterA bson
Documentrepresenting the match criteria.updateA bson
Documentrepresenting the update to be applied to a matching document.optionsRemoteFindOneAndModifyOptionsto use when executing the command.Return Value
A publisher that eventually return
Documentornilif document wasn’t found orError. -
Updates a single document in a collection based on a query filter and returns the document in either its pre-update or post-update form. Unlike
updateOneDocument, this action allows you to atomically find, update, and return a document with the same command. This avoids the risk of other update operations changing the document between separate find and update operations.Declaration
Parameters
filterA bson
Documentrepresenting the match criteria.updateA bson
Documentrepresenting the update to be applied to a matching document.Return Value
A publisher that eventually return
Documentornilif document wasn’t found orError. -
Overwrites a single document in a collection based on a query filter and returns the document in either its pre-replacement or post-replacement form. Unlike
updateOneDocument, this action allows you to atomically find, replace, and return a document with the same command. This avoids the risk of other update operations changing the document between separate find and update operations.Declaration
Swift
func findOneAndReplace(filter: Document, replacement: Document, options: FindOneAndModifyOptions) -> Future<Document?, Error>Parameters
filterA
Documentthat should match the query.replacementA
Documentdescribing the replacement.optionsFindOneAndModifyOptionsto use when executing the command.Return Value
A publisher that eventually return
Documentornilif document wasn’t found orError. -
Overwrites a single document in a collection based on a query filter and returns the document in either its pre-replacement or post-replacement form. Unlike
updateOneDocument, this action allows you to atomically find, replace, and return a document with the same command. This avoids the risk of other update operations changing the document between separate find and update operations.Declaration
Parameters
filterA
Documentthat should match the query.replacementA
Documentdescribing the replacement.Return Value
A publisher that eventually return
Documentornilif document wasn’t found orError. -
Removes a single document from a collection based on a query filter and returns a document with the same form as the document immediately before it was deleted. Unlike
deleteOneDocument, this action allows you to atomically find and delete a document with the same command. This avoids the risk of other update operations changing the document between separate find and delete operations.Declaration
Swift
func findOneAndDelete(filter: Document, options: FindOneAndModifyOptions) -> Future<Document?, Error>Parameters
filterA
Documentthat should match the query.optionsFindOneAndModifyOptionsto use when executing the command.Return Value
A publisher that eventually return
Documentornilif document wasn’t found orError. -
Removes a single document from a collection based on a query filter and returns a document with the same form as the document immediately before it was deleted. Unlike
deleteOneDocument, this action allows you to atomically find and delete a document with the same command. This avoids the risk of other update operations changing the document between separate find and delete operations.Parameters
filterA
Documentthat should match the query.Return Value
A publisher that eventually return
Documentornilif document wasn’t found orError.
View on GitHub
Install in Dash