RLMMongoCollection

Objective-C


@interface RLMMongoCollection : NSObject

Swift

@_nonSendable(_assumed) class RLMMongoCollection : NSObject, @unchecked Sendable

RLMMongoCollection表示 MongoDB collection。

您可以从RLMMongoDatabase获取一个实例。

提供创建、读取、更新和删除方法。

针对 Realm Cloud 服务器的操作是异步执行的。

注意

用户必须先登录,然后才能读取或写入数据。
  • 用法:RLLMongoClient *client = [self.app mongoClient:@“ MongoDB 1”]; RLMMongoDatabase *database = [client databaseWithName:@“test_data”]; RLMMongoCollection *collection = [database collectionWithName:@“Dog”]; [collection insertOneDocument:@{@“name”: @“fido”, @“breed”: @“cane Corso”} completed:…];

  • 此MongoDB集合的名称。

    声明

    Objective-C

    @property (nonatomic, readonly) NSString *_Nonnull name;

    Swift

    var name: String { get }
  • 将提供的值编码为 BSON 并将其插入。 如果该值缺少标识符,则会为其生成一个标识符。

    声明

    Objective-C

    - (void)insertOneDocument:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)document
                   completion:(nonnull RLMMongoInsertBlock)completion;

    参数

    document

    要插入的Document值。

    completion

    尝试执行插入操作的结果。 成功后将返回插入对象的 Id

  • 将提供的值编码为 BSON 并插入。 如果有任何值缺少标识符,则会生成这些值。

    声明

    Objective-C

    - (void)insertManyDocuments:
                (nonnull NSArray<NSDictionary<NSString *, id<RLMBSON>> *> *)
                    documents
                     completion:(nonnull RLMMongoInsertManyBlock)completion;

    参数

    documents

    要插入的 bson 数组中的Document值。

    completion

    插入的结果,按顺序返回插入文档ID 的大量

  • 查找此collection中与提供的筛选器匹配的文档。

    声明

    Objective-C

    - (void)findWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
              options:(nonnull RLMFindOptions *)options
           completion:(nonnull RLMMongoFindBlock)completion;

    参数

    filterDocument

    作为 bson 的Document ,应与查询匹配。

    options

    RLMFindOptions 执行命令时使用。

    completion

    生成的 bson 文档数组或错误(如果发生)

  • 查找此collection中与提供的筛选器匹配的文档。

    声明

    Objective-C

    - (void)findWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
           completion:(nonnull RLMMongoFindBlock)completion;

    参数

    filterDocument

    作为 bson 的Document ,应与查询匹配。

    completion

    生成的BSON数组为string或错误,如果发生

  • 从与提供的筛选器匹配的集合或视图中返回一个文档。 如果多个文档满足查询条件,则此方法根据查询的排序顺序或自然顺序返回第一个文档。

    声明

    Objective-C

    - (void)findOneDocumentWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                         options:(nonnull RLMFindOptions *)options
                      completion:(nonnull RLMMongoFindOneBlock)completion;

    参数

    filterDocument

    作为 bson 的Document ,应与查询匹配。

    options

    RLMFindOptions 执行命令时使用。

    completion

    如果发生以下情况,则产生的 bson 或错误:

  • 从与提供的筛选器匹配的集合或视图中返回一个文档。 如果多个文档满足查询条件,则此方法根据查询的排序顺序或自然顺序返回第一个文档。

    声明

    Objective-C

    - (void)findOneDocumentWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                      completion:(nonnull RLMMongoFindOneBlock)completion;

    参数

    filterDocument

    作为 bson 的Document ,应与查询匹配。

    completion

    如果发生以下情况,则产生的 bson 或错误:

  • 针对此collection运行聚合框架管道。

    声明

    Objective-C

    - (void)aggregateWithPipeline:
                (nonnull NSArray<NSDictionary<NSString *, id<RLMBSON>> *> *)pipeline
                       completion:(nonnull RLMMongoFindBlock)completion;

    参数

    pipeline

    Documents组成的 bson 数组,其中包含要执行的聚合操作的管道。

    completion

    生成的 bson 文档数组或错误(如果发生)

  • 计算此集合中与提供的筛选器匹配的文档数量。

    声明

    Objective-C

    - (void)countWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                 limit:(NSInteger)limit
            completion:(nonnull RLMMongoCountBlock)completion;

    参数

    filterDocument

    作为 bson 的Document ,应与查询匹配。

    limit

    要计数的最大文档数

    completion

    返回与筛选器匹配的文档计数。

  • 计算此集合中与提供的筛选器匹配的文档数量。

    声明

    Objective-C

    - (void)countWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
            completion:(nonnull RLMMongoCountBlock)completion;

    参数

    filterDocument

    作为 bson 的Document ,应与查询匹配。

    completion

    返回与筛选器匹配的文档计数。

  • 从collection中删除单个匹配文档。

    声明

    Objective-C

    - (void)deleteOneDocumentWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                        completion:(nonnull RLMMongoCountBlock)completion;

    参数

    filterDocument

    作为 bson 的Document ,应与查询匹配。

    completion

    执行删除的结果。 返回已删除对象的计数

  • 删除多个文档

    声明

    Objective-C

    - (void)deleteManyDocumentsWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                          completion:(nonnull RLMMongoCountBlock)completion;

    参数

    filterDocument

    表示匹配条件的文档

    completion

    执行删除的结果。 返回删除操作的计数

  • 更新与此collection中提供的筛选器匹配的单个文档。

    声明

    Objective-C

    - (void)updateOneDocumentWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                    updateDocument:(nonnull NSDictionary<NSString *, id<RLMBSON>> *)
                                       updateDocument
                            upsert:(BOOL)upsert
                        completion:(nonnull RLMMongoUpdateBlock)completion;

    参数

    filterDocument

    表示匹配条件的 bson Document

    updateDocument

    一个 bson Document ,表示要应用于匹配文档的更新。

    upsert

    如果为 true,则在没有文档与查询匹配的情况下创建新文档。

    completion

    尝试更新文档的结果。

  • 更新与此collection中提供的筛选器匹配的单个文档。

    声明

    Objective-C

    - (void)updateOneDocumentWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                    updateDocument:(nonnull NSDictionary<NSString *, id<RLMBSON>> *)
                                       updateDocument
                        completion:(nonnull RLMMongoUpdateBlock)completion;

    参数

    filterDocument

    表示匹配条件的 bson Document

    updateDocument

    一个 bson Document ,表示要应用于匹配文档的更新。

    completion

    尝试更新文档的结果。

  • 更新与此collection中提供的筛选器匹配的多个文档。

    声明

    Objective-C

    - (void)updateManyDocumentsWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                      updateDocument:
                          (nonnull NSDictionary<NSString *, id<RLMBSON>> *)
                              updateDocument
                              upsert:(BOOL)upsert
                          completion:(nonnull RLMMongoUpdateBlock)completion;

    参数

    filterDocument

    表示匹配条件的 bson Document

    updateDocument

    一个 bson Document ,表示要应用于匹配文档的更新。

    upsert

    如果为 true,则在没有文档与查询匹配的情况下创建新文档。

    completion

    尝试更新文档的结果。

  • 更新与此collection中提供的筛选器匹配的多个文档。

    声明

    Objective-C

    - (void)updateManyDocumentsWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                      updateDocument:
                          (nonnull NSDictionary<NSString *, id<RLMBSON>> *)
                              updateDocument
                          completion:(nonnull RLMMongoUpdateBlock)completion;

    参数

    filterDocument

    表示匹配条件的 bson Document

    updateDocument

    一个 bson Document ,表示要应用于匹配文档的更新。

    completion

    尝试更新文档的结果。

  • 根据查询筛选器更新集合中的单个文档,并以其更新前或更新后的形式返回该文档。 与updateOneDocument不同,此操作允许您使用相同命令以原子方式查找、更新和返回文档。 这样可以避免其他更新操作在单独的查找和更新操作之间更改文档的风险。

    声明

    Objective-C

    - (void)findOneAndUpdateWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                   updateDocument:(nonnull NSDictionary<NSString *, id<RLMBSON>> *)
                                      updateDocument
                          options:(nonnull RLMFindOneAndModifyOptions *)options
                       completion:(nonnull RLMMongoFindOneBlock)completion;

    参数

    filterDocument

    表示匹配条件的 bson Document

    updateDocument

    一个 bson Document ,表示要应用于匹配文档的更新。

    options

    RemoteFindOneAndModifyOptions 执行命令时使用。

    completion

    尝试更新文档的结果。

  • 根据查询筛选器更新集合中的单个文档,并以其更新前或更新后的形式返回该文档。 与updateOneDocument不同,此操作允许您使用相同命令以原子方式查找、更新和返回文档。 这样可以避免其他更新操作在单独的查找和更新操作之间更改文档的风险。

    声明

    Objective-C

    - (void)findOneAndUpdateWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                   updateDocument:(nonnull NSDictionary<NSString *, id<RLMBSON>> *)
                                      updateDocument
                       completion:(nonnull RLMMongoFindOneBlock)completion;

    参数

    filterDocument

    表示匹配条件的 bson Document

    updateDocument

    一个 bson Document ,表示要应用于匹配文档的更新。

    completion

    尝试更新文档的结果。

  • 根据查询筛选器覆盖collection中的单个文档,并以其帖子替换前或替换帖子的形式返回该文档。与updateOneDocument不同,此操作允许您使用相同命令以原子方式查找、替换和返回文档。 这样可以避免其他更新操作在单独的查找和更新操作之间更改文档的风险。

    声明

    Objective-C

    - (void)findOneAndReplaceWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
               replacementDocument:(nonnull NSDictionary<NSString *, id<RLMBSON>> *)
                                       replacementDocument
                           options:(nonnull RLMFindOneAndModifyOptions *)options
                        completion:(nonnull RLMMongoFindOneBlock)completion;

    参数

    filterDocument

    应与查询匹配的Document

    replacementDocument

    描述替换项的Document

    options

    RLMFindOneAndModifyOptions 执行命令时使用。

    completion

    尝试替换文档的结果。

  • 根据查询筛选器覆盖collection中的单个文档,并以其帖子替换前或替换帖子的形式返回该文档。与updateOneDocument不同,此操作允许您使用相同命令以原子方式查找、替换和返回文档。 这样可以避免其他更新操作在单独的查找和更新操作之间更改文档的风险。

    声明

    Objective-C

    - (void)findOneAndReplaceWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
               replacementDocument:(nonnull NSDictionary<NSString *, id<RLMBSON>> *)
                                       replacementDocument
                        completion:(nonnull RLMMongoFindOneBlock)completion;

    参数

    filterDocument

    应与查询匹配的Document

    replacementDocument

    描述更新的Document

    completion

    尝试替换文档的结果。

  • 根据查询筛选器从集合中删除单个文档,并返回与删除前的文档具有相同格式的文档。 与deleteOneDocument不同,此操作允许您使用相同命令以原子方式查找和删除文档。 这样可以避免其他更新操作在单独的查找和删除操作之间更改文档的风险。

    声明

    Objective-C

    - (void)findOneAndDeleteWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                          options:(nonnull RLMFindOneAndModifyOptions *)options
                       completion:(nonnull RLMMongoDeleteBlock)completion;

    参数

    filterDocument

    应与查询匹配的Document

    options

    RLMFindOneAndModifyOptions 执行命令时使用。

    completion

    尝试删除文档的结果。

  • 根据查询筛选器从集合中删除单个文档,并返回与删除前的文档具有相同格式的文档。 与deleteOneDocument不同,此操作允许您使用相同命令以原子方式查找和删除文档。 这样可以避免其他更新操作在单独的查找和删除操作之间更改文档的风险。

    声明

    Objective-C

    - (void)findOneAndDeleteWhere:
                (nonnull NSDictionary<NSString *, id<RLMBSON>> *)filterDocument
                       completion:(nonnull RLMMongoDeleteBlock)completion;

    参数

    filterDocument

    应与查询匹配的Document

    completion

    尝试删除文档的结果。

  • 打开针对集合的 MongoDB 变更流以监视更改。 生成的流将收到有关此集合的所有事件的通知,根据配置的 MongoDB 规则,授权活动用户查看这些事件。

    声明

    Objective-C

    - (nonnull RLMChangeStream *)watchWithDelegate:
                                     (nonnull id<RLMChangeEventDelegate>)delegate
                                     delegateQueue:(nullable dispatch_queue_t)queue;

    参数

    delegate

    对生成的变更流中的事件和错误做出反应的委托。

    queue

    将流媒体事件分派到可选队列,如果未提供队列,则使用主队列

  • 打开针对集合的 MongoDB 变更流,以监视对特定文档所做的更改。 要监视的文档必须由其 _id 明确指定。

    声明

    Objective-C

    - (nonnull RLMChangeStream *)
        watchWithFilterIds:(nonnull NSArray<RLMObjectId *> *)filterIds
                  delegate:(nonnull id<RLMChangeEventDelegate>)delegate
             delegateQueue:(nullable dispatch_queue_t)queue;

    参数

    filterIds

    要监视的集合中的 _id 列表。

    delegate

    对生成的变更流中的事件和错误做出反应的委托。

    queue

    将流媒体事件分派到可选队列,如果未提供队列,则使用主队列

  • 打开针对collection的MongoDB change stream以监视更改。提供的 BSON 文档将用作来自流的变更事件的匹配表达式筛选器。

    请参阅https://mongodb.com/zh-cn/docs/manual/reference/operator/aggregation/match/ 了解有关如何定义匹配筛选器的文档。

    定义用于筛选 ChangeEvent 的匹配表达式与为Atlas Triggers定义匹配表达式类似: https ://mongodb.com/zh-cn/docs/realm/triggers/database-triggers/

    声明

    Objective-C

    - (nonnull RLMChangeStream *)
        watchWithMatchFilter:
            (nonnull NSDictionary<NSString *, id<RLMBSON>> *)matchFilter
                    delegate:(nonnull id<RLMChangeEventDelegate>)delegate
               delegateQueue:(nullable dispatch_queue_t)queue;

    参数

    matchFilter

    应用于传入变更事件的 $match 筛选器

    delegate

    对生成的变更流中的事件和错误做出反应的委托。

    queue

    将流媒体事件分派到可选队列,如果未提供队列,则使用主队列