【问题标题】:Mongodb- How to get data from a partition that was created by other user?Mongodb-如何从其他用户创建的分区中获取数据?
【发布时间】:2021-09-24 08:28:36
【问题描述】:

我正在为我的 Android 应用程序使用 Realm MongoDB,但我遇到了一个问题:

我的应用中有不同的用户,每个用户都有自己的“卡片”。每个用户卡的分区为: “卡 = 用户 ID”。 所以,我希望能够将一张卡从一个用户发送给另一个用户。我通过包含用户 ID 和特定卡 ID 的链接来完成。

所以我的代码看起来像:

    Realm.init(this);
    mainApp = new App(new AppConfiguration.Builder(APP_ID).defaultSyncErrorHandler((session, error) ->
            Log.e("TAG()", "Sync error: ${error.errorMessage}")
    ).build());

    //TEMP CODE
    String partition = "Card=611d7n582w36796ce34af106"; //test partition of another user

    if(mainApp.currentUser() != null) {
        SyncConfiguration config = new SyncConfiguration.Builder(
                mainApp.currentUser(),
                partition)
                .build();

        Realm realmLinkCard = Realm.getInstance(config);

        Log.d(TAG, "onCreate: cards found- " + realmLinkCard.where(Card.class).findAll().size());
    }

最后一个日志总是显示 0。我知道肯定有卡片,因为如果创建相应分区的用户已登录,那么它确实会找到卡片。

整个同步的读写权限都设置为 true。

可能是什么问题?

【问题讨论】:

    标签: android mongodb realm


    【解决方案1】:

    您不能由具有不同分区的用户访问领域。

    相反,您可以创建一个 mongodb 函数并从您的用户那里调用它。

    在这里制作你的功能:

    在这里查看How to create a function

    并通过查看How to call a function from client 来调用它

    领域函数的快速示例:

    exports = async function funcName(partition) {
      const cluster = context.services.get('myclustername');
      const mycollection = cluster.db('mydbname').collection('mycollectionname');
      let result = [];
    
      try {
        result = mycollection.findOne({
          _partition: partition,
        });
      } catch (e) {
        result.push(e);
        return result;
      }
    
      return result;
    };
    

    要调用它,请参阅上面的文档,因为我不是 Android 开发人员。

    【讨论】:

    • 感谢您的回答,我实际上解决了我的问题。问题出在同步配置上,我添加了以下行: .waitForInitialRemoteData() 强制 Realm 在继续之前同步所有数据,因为显然领域在后台同步了数据,如果我试图获取尚未得到的数据已同步,但它只会显示为不存在,这就是问题所在
    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多