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) -> ChangeStream
Parameters
delegate
The delegate that will react to events and errors from the resulting change stream.
queue
Dispatches 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) -> ChangeStream
Parameters
matchFilter
The $match filter to apply to incoming change events
delegate
The delegate that will react to events and errors from the resulting change stream.
queue
Dispatches 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) -> ChangeStream
Parameters
filterIds
The list of _ids in the collection to watch.
delegate
The delegate that will react to events and errors from the resulting change stream.
queue
Dispatches 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
document
document A
Document
value to insert.completion
The 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
documents
The
Document
values in a bson array to insert.completion
The 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
filter
A
Document
as bson that should match the query.options
FindOptions
to use when executing the command.completion
The 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
filter
A
Document
as bson that should match the query.options
FindOptions
to use when executing the command.completion
The 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
pipeline
A bson array made up of
Documents
containing the pipeline of aggregation operations to perform.completion
The 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
filter
A
Document
as bson that should match the query.limit
The max amount of documents to count
completion
Returns 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
filter
A
Document
as bson that should match the query.completion
The 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
filter
Document representing the match criteria
completion
The 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
filter
A
Document
that should match the query.replacement
A
Document
describing the replacement.options
FindOneAndModifyOptions
to use when executing the command.completion
The 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
filter
A
Document
that should match the query.options
FindOneAndModifyOptions
to use when executing the command.completion
The 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
document
A
Document
value 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
documents
The
Document
values in a bson array to insert.Return Value
The object ids of inserted documents.
-
find(filter:
Asynchronousoptions: ) Finds the documents in this collection which match the provided filter.
Declaration
Swift
@_unsafeInheritExecutor public func find(filter: Document, options: FindOptions? = nil) async throws -> [Document]
Parameters
filter
A
Document
as bson that should match the query.options
FindOptions
to use when executing the command.Return Value
Array of
Document
filtered. -
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
@_unsafeInheritExecutor public func findOneDocument(filter: Document, options: FindOptions? = nil) async throws -> Document?
Parameters
filter
A
Document
as bson that should match the query.options
FindOptions
to use when executing the command.Return Value
Document
filtered. -
aggregate(pipeline:
Asynchronous) Runs an aggregation framework pipeline against this collection.
Parameters
pipeline
A bson array made up of
Documents
containing the pipeline of aggregation operations to perform.Return Value
An array of
Document
result 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 -> Int
Parameters
filter
A
Document
as bson that should match the query.limit
The 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 -> Int
Parameters
filter
Document representing the match criteria
Return Value
Int
count 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 -> UpdateResult
Parameters
filter
A bson
Document
representing the match criteria.update
A bson
Document
representing the update to be applied to a matching document.upsert
When true, creates a new document if no document matches the query.
Return Value
UpdateResult
result of theupdateOne
operation. -
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 -> UpdateResult
Parameters
filter
A bson
Document
representing the match criteria.update
A bson
Document
representing the update to be applied to a matching document.upsert
When true, creates a new document if no document matches the query.
Return Value
UpdateResult
result of theupdateMany
operation. -
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
@_unsafeInheritExecutor public func findOneAndUpdate(filter: Document, update: Document, options: FindOneAndModifyOptions? = nil) async throws -> Document?
Parameters
filter
A bson
Document
representing the match criteria.update
A bson
Document
representing the update to be applied to a matching document.options
RemoteFindOneAndModifyOptions
to use when executing the command.Return Value
Document
result of the attempt to update a document ornil
if 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
@_unsafeInheritExecutor public func findOneAndReplace(filter: Document, replacement: Document, options: FindOneAndModifyOptions? = nil) async throws -> Document?
Parameters
filter
A
Document
that should match the query.replacement
A
Document
describing the replacement.options
FindOneAndModifyOptions
to use when executing the command.Return Value
Document
result of the attempt to reaplce a document ornil
if 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
@_unsafeInheritExecutor public func findOneAndDelete(filter: Document, options: FindOneAndModifyOptions? = nil) async throws -> Document?
Parameters
filter
A
Document
that should match the query.options
FindOneAndModifyOptions
to use when executing the command.Return Value
Document
result of the attempt to delete a document ornil
if 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.WatchPublisher
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(filterIds: [ObjectId]) -> Publishers.WatchPublisher
Parameters
filterIds
The 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.WatchPublisher
Parameters
matchFilter
The $match filter to apply to incoming change events.
Return Value
A publisher that emits the AnyBSON change event each time the collection changes.
-
An async sequence of AnyBSON values containing information about each change to the MongoDB collection
Declaration
Swift
public var changeEvents: AsyncThrowingPublisher<Publishers.WatchPublisher> { get }
-
An async sequence of AnyBSON values containing information about each change to the MongoDB collection
Declaration
Swift
public func changeEvents(onOpen: @Sendable @escaping () -> Void) -> AsyncThrowingPublisher<Publishers.WatchPublisher>
Parameters
onOpen
A callback which is invoked when the watch stream has initialized on the server. Server-side changes triggered before this callback is invoked may not produce change events.
-
An async sequence of AnyBSON values containing information about each change to objects with ids contained in
filterIds
within the the MongoDB collection.Declaration
Swift
public func changeEvents(filterIds: [ObjectId], onOpen: (@Sendable () -> Void)? = nil) -> AsyncThrowingPublisher<Publishers.WatchPublisher>
Parameters
filterIds
Document ids which should produce change events
onOpen
An optional callback which is invoked when the watch stream has initialized on the server. Server-side changes triggered before this callback is invoked may not produce change events.
-
An async sequence of AnyBSON values containing information about each change to objects within the MongoDB collection matching the given $match filter.
Declaration
Swift
public func changeEvents(matchFilter: Document, onOpen: (@Sendable () -> Void)? = nil) -> AsyncThrowingPublisher<Publishers.WatchPublisher>
Parameters
matchFilter
$match filter to filter the documents which produce change events.
onOpen
An optional callback which is invoked when the watch stream has initialized on the server. Server-side changes triggered before this callback is invoked may not produce change events.
-
Encodes the provided value to BSON and inserts it. If the value is missing an identifier, one will be generated for it.
Parameters
document
A
Document
value 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
documents
The
Document
values 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
filter
A
Document
as bson that should match the query.options
FindOptions
to 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
filter
A
Document
as 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
filter
A
Document
as bson that should match the query.options
FindOptions
to use when executing the command.Return Value
A publisher that eventually return
Document
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.
Parameters
filter
A
Document
as bson that should match the query.Return Value
A publisher that eventually return
Document
ornil
if document wasn’t found orError
. -
Runs an aggregation framework pipeline against this collection.
Parameters
pipeline
A bson array made up of
Documents
containing the pipeline of aggregation operations to perform.Return Value
A publisher that eventually return
Document
orError
. -
Counts the number of documents in this collection matching the provided filter.
Declaration
Swift
func count(filter: Document, limit: Int) -> Future<Int, Error>
Parameters
filter
A
Document
as bson that should match the query.limit
The max amount of documents to count
Return Value
A publisher that eventually return
Int
count of documents orError
. -
Deletes multiple documents
Declaration
Swift
func deleteManyDocuments(filter: Document) -> Future<Int, Error>
Parameters
filter
Document representing the match criteria
Return Value
A publisher that eventually return
Int
count 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
filter
A bson
Document
representing the match criteria.update
A bson
Document
representing the update to be applied to a matching document.upsert
When true, creates a new document if no document matches the query.
Return Value
A publisher that eventually return
UpdateResult
orError
. -
Updates a single document matching the provided filter in this collection.
Declaration
Swift
func updateOneDocument(filter: Document, update: Document) -> Future<UpdateResult, Error>
Parameters
filter
A bson
Document
representing the match criteria.update
A bson
Document
representing the update to be applied to a matching document.Return Value
A publisher that eventually return
UpdateResult
orError
. -
Updates multiple documents matching the provided filter in this collection.
Declaration
Swift
func updateManyDocuments(filter: Document, update: Document, upsert: Bool) -> Future<UpdateResult, Error>
Parameters
filter
A bson
Document
representing the match criteria.update
A bson
Document
representing the update to be applied to a matching document.upsert
When true, creates a new document if no document matches the query.
Return Value
A publisher that eventually return
UpdateResult
orError
. -
Updates multiple documents matching the provided filter in this collection.
Declaration
Swift
func updateManyDocuments(filter: Document, update: Document) -> Future<UpdateResult, Error>
Parameters
filter
A bson
Document
representing the match criteria.update
A bson
Document
representing the update to be applied to a matching document.Return Value
A publisher that eventually return
UpdateResult
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
Swift
func findOneAndUpdate(filter: Document, update: Document, options: FindOneAndModifyOptions) -> Future<Document?, Error>
Parameters
filter
A bson
Document
representing the match criteria.update
A bson
Document
representing the update to be applied to a matching document.options
RemoteFindOneAndModifyOptions
to use when executing the command.Return Value
A publisher that eventually return
Document
ornil
if 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
filter
A bson
Document
representing the match criteria.update
A bson
Document
representing the update to be applied to a matching document.Return Value
A publisher that eventually return
Document
ornil
if 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
filter
A
Document
that should match the query.replacement
A
Document
describing the replacement.options
FindOneAndModifyOptions
to use when executing the command.Return Value
A publisher that eventually return
Document
ornil
if 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
filter
A
Document
that should match the query.replacement
A
Document
describing the replacement.Return Value
A publisher that eventually return
Document
ornil
if 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
filter
A
Document
that should match the query.options
FindOneAndModifyOptions
to use when executing the command.Return Value
A publisher that eventually return
Document
ornil
if 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
filter
A
Document
that should match the query.Return Value
A publisher that eventually return
Document
ornil
if document wasn’t found orError
.