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

    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

    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

    public func find(filter: Document,
                     options: 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

  • Finds the documents in this collection which match the provided filter.

    Declaration

    Swift

    public func find(filter: Document,
                     _ completion: @escaping MongoFindBlock)

    Parameters

    filter

    A Document as bson that should match the query.

    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

    public func findOneDocument(filter: Document,
                                options: 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

  • 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,
                                _ completion: @escaping MongoFindOneBlock)

    Parameters

    filter

    A Document as bson that should match the query.

    completion

    The resulting bson or error if one occurs

  • Runs an aggregation framework pipeline against this collection.

    Declaration

    Swift

    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

    public func count(filter: Document,
                      limit: Int,
                      _ 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.

  • Counts the number of documents in this collection matching the provided filter.

    Declaration

    Swift

    public func count(filter: Document,
                      _ completion: @escaping MongoCountBlock)

    Parameters

    filter

    A Document as bson that should match the query.

    completion

    Returns the count of the documents that matched the filter.

  • Deletes a single matching document from the collection.

    Declaration

    Swift

    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

    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

    public func updateOneDocument(filter: Document,
                                  update: Document,
                                  upsert: Bool,
                                  _ completion: @escaping MongoUpdateBlock)

    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.

    completion

    The result of the attempt to update a document.

  • Updates a single document matching the provided filter in this collection.

    Declaration

    Swift

    public func updateOneDocument(filter: Document,
                                  update: Document,
                                  _ completion: @escaping MongoUpdateBlock)

    Parameters

    filter

    A bson Document representing the match criteria.

    update

    A bson Document representing the update to be applied to a matching document.

    completion

    The result of the attempt to update a document.

  • Updates multiple documents matching the provided filter in this collection.

    Declaration

    Swift

    public func updateManyDocuments(filter: Document,
                                    update: Document,
                                    upsert: Bool,
                                    _ completion: @escaping MongoUpdateBlock)

    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.

    completion

    The result of the attempt to update a document.

  • Updates multiple documents matching the provided filter in this collection.

    Declaration

    Swift

    public func updateManyDocuments(filter: Document,
                                    update: Document,
                                    _ completion: @escaping MongoUpdateBlock)

    Parameters

    filter

    A bson Document representing the match criteria.

    update

    A bson Document representing the update to be applied to a matching document.

    completion

    The result of the attempt to update a document.

  • 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,
                                 _ completion: @escaping MongoFindOneBlock)

    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.

    completion

    The result of the attempt to update a document.

  • 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,
                                 _ completion: @escaping MongoFindOneBlock)

    Parameters

    filter

    A bson Document representing the match criteria.

    update

    A bson Document representing the update to be applied to a matching document.

    completion

    The result of the attempt to update a document.

  • 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,
                                  _ 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.

  • 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,
                                  _ completion: @escaping MongoFindOneBlock)

    Parameters

    filter

    A Document that should match the query.

    replacement

    A Document describing the replacement.

    options

    RLMFindOneAndModifyOptions 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

    public func findOneAndDelete(filter: Document,
                                 options: FindOneAndModifyOptions,
                                 _ 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.

  • 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,
                                 _ completion: @escaping MongoFindOneBlock)

    Parameters

    filter

    A Document that should match the query.

    completion

    The result of the attempt to delete a document.

  • 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.

  • Encodes the provided value to BSON and inserts it. If the value is missing an identifier, one will be generated for it. @param document: A Document value to insert. @returns A publisher that eventually return the object id of the inserted document or Error.

    Declaration

    Swift

    func insertOne(_ document: Document) -> Future<AnyBSON, Error>
  • Encodes the provided values to BSON and inserts them. If any values are missing identifiers, they will be generated. @param documents: The Document values in a bson array to insert. @returns A publisher that eventually return the object ids of inserted documents or Error.

    Declaration

    Swift

    func insertMany(_ documents: [Document]) -> Future<[AnyBSON], Error>
  • Finds the documents in this collection which match the provided filter. @param filter: A Document as bson that should match the query. @param options: FindOptions to use when executing the command. @returns A publisher that eventually return [ObjectId] of documents or Error.

    Declaration

    Swift

    func find(filter: Document, options: FindOptions) -> Future<[Document], Error>
  • Finds the documents in this collection which match the provided filter. @param filter: A Document as bson that should match the query. @returns A publisher that eventually return [ObjectId] of documents or Error.

    Declaration

    Swift

    func find(filter: Document) -> Future<[Document], Error>
  • 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. @param filter: A Document as bson that should match the query. @param options: FindOptions to use when executing the command. @returns A publisher that eventually return Document or Error.

    Declaration

    Swift

    func findOneDocument(filter: Document, options: FindOptions) -> Future<Document?, Error>
  • 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. @param filter: A Document as bson that should match the query. @returns A publisher that eventually return Document or nil if document wasn’t found or Error.

    Declaration

    Swift

    func findOneDocument(filter: Document) -> Future<Document?, Error>
  • Runs an aggregation framework pipeline against this collection. @param pipeline: A bson array made up of Documents containing the pipeline of aggregation operations to perform. @returns A publisher that eventually return Document or Error.

    Declaration

    Swift

    func aggregate(pipeline: [Document]) -> Future<[Document], Error>
  • Counts the number of documents in this collection matching the provided filter. @param filter: A Document as bson that should match the query. @param limit: The max amount of documents to count @returns A publisher that eventually return Int count of documents or Error.

    Declaration

    Swift

    func count(filter: Document, limit: Int) -> Future<Int, Error>
  • Counts the number of documents in this collection matching the provided filter. @param filter: A Document as bson that should match the query. @returns A publisher that eventually return Int count of documents or Error.

    Declaration

    Swift

    func count(filter: Document) -> Future<Int, Error>
  • Deletes a single matching document from the collection. @param filter: A Document as bson that should match the query. @returns A publisher that eventually return Int count of deleted documents or Error.

    Declaration

    Swift

    func deleteOneDocument(filter: Document) -> Future<Int, Error>
  • Deletes multiple documents @param filter: Document representing the match criteria @returns A publisher that eventually return Int count of deleted documents or Error.

    Declaration

    Swift

    func deleteManyDocuments(filter: Document) -> Future<Int, Error>
  • Updates a single document matching the provided filter in this collection. @param filter: A bson Document representing the match criteria. @param update: A bson Document representing the update to be applied to a matching document. @param upsert: When true, creates a new document if no document matches the query. @returns A publisher that eventually return UpdateResult or Error.

    Declaration

    Swift

    func updateOneDocument(filter: Document, update: Document, upsert: Bool) -> Future<UpdateResult, Error>
  • Updates a single document matching the provided filter in this collection. @param filter: A bson Document representing the match criteria. @param update: A bson Document representing the update to be applied to a matching document. @returns A publisher that eventually return UpdateResult or Error.

    Declaration

    Swift

    func updateOneDocument(filter: Document, update: Document) -> Future<UpdateResult, Error>
  • Updates multiple documents matching the provided filter in this collection. @param filter: A bson Document representing the match criteria. @param update: A bson Document representing the update to be applied to a matching document. @param upsert: When true, creates a new document if no document matches the query. @returns A publisher that eventually return UpdateResult or Error.

    Declaration

    Swift

    func updateManyDocuments(filter: Document, update: Document, upsert: Bool) -> Future<UpdateResult, Error>
  • Updates multiple documents matching the provided filter in this collection. @param filter: A bson Document representing the match criteria. @param update: A bson Document representing the update to be applied to a matching document. @returns A publisher that eventually return UpdateResult or Error.

    Declaration

    Swift

    func updateManyDocuments(filter: Document, update: Document) -> Future<UpdateResult, Error>
  • 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. @param filter: A bson Document representing the match criteria. @param update: A bson Document representing the update to be applied to a matching document. @param options: RemoteFindOneAndModifyOptions to use when executing the command. @returns A publisher that eventually return Document or nil if document wasn’t found or Error.

    Declaration

    Swift

    func findOneAndUpdate(filter: Document, update: Document, options: FindOneAndModifyOptions) -> Future<Document?, Error>
  • 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. @param filter: A bson Document representing the match criteria. @param update: A bson Document representing the update to be applied to a matching document. @returns A publisher that eventually return Document or nil if document wasn’t found or Error.

    Declaration

    Swift

    func findOneAndUpdate(filter: Document, update: Document) -> Future<Document?, Error>
  • 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. @param filter: A Document that should match the query. @param replacement: A Document describing the replacement. @param options: FindOneAndModifyOptions to use when executing the command. @returns A publisher that eventually return Document or nil if document wasn’t found or Error.

    Declaration

    Swift

    func findOneAndReplace(filter: Document, replacement: Document, options: FindOneAndModifyOptions) -> Future<Document?, Error>
  • 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. @param filter: A Document that should match the query. @param replacement: A Document describing the replacement. @returns A publisher that eventually return Document or nil if document wasn’t found or Error.

    Declaration

    Swift

    func findOneAndReplace(filter: Document, replacement: Document) -> Future<Document?, Error>
  • 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. @param filter: A Document that should match the query. @param options: FindOneAndModifyOptions to use when executing the command. @returns A publisher that eventually return Document or nil if document wasn’t found or Error.

    Declaration

    Swift

    func findOneAndDelete(filter: Document, options: FindOneAndModifyOptions) -> Future<Document?, Error>
  • 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. @param filter: A Document that should match the query. @returns A publisher that eventually return Document or nil if document wasn’t found or Error.

    Declaration

    Swift

    func findOneAndDelete(filter: Document) -> Future<Document?, Error>