【问题标题】:Parse server - get related classes - rest api解析服务器-获取相关类-rest api
【发布时间】:2020-05-25 17:28:37
【问题描述】:

假设我有一个类 User 和一个类 Profile

配置文件类有一个名为“sex”的字段和一个名为“user”的字段,它是一个指向用户类的指针。

如果我通过https://myapi.back4app.io/classes/Profile 获得配置文件端点,我可以获得配置文件对象:

{
    "results": [
        {
            "objectId": "sIE6lOZP7R",
            "user": {
                "__type": "Pointer",
                "className": "_User",
                "objectId": "asP3EFYSR4"
            },
            "sex": "male",
            "createdAt": "2020-05-25T17:15:49.324Z",
            "updatedAt": "2020-05-25T17:15:49.324Z"
        }
    ]
}

如果我想包含此个人资料的用户,我可以包含:https://myapi.back4app.io/classes/Perfil?include=user 所以我得到:

{
    "results": [
        {
            "objectId": "sIE6lOZP7R",
            "user": {
                "objectId": "asP3EFYSR4",
                "username": "fabiojansen",
                "createdAt": "2020-05-25T17:15:16.273Z",
                "updatedAt": "2020-05-25T17:15:16.273Z",
                "ACL": {
                    "*": {
                        "read": true
                    },
                    "asP3EFYSR4": {
                        "read": true,
                        "write": true
                    }
                },
                "__type": "Object",
                "className": "_User"
            },
            "sex": "male",
            "createdAt": "2020-05-25T17:15:49.324Z",
            "updatedAt": "2020-05-25T17:15:49.324Z"
        }
    ]
}

没关系,但如果我想在一个查询中获取所有用户的个人资料信息?这是可能的?在我的 User 类中,我没有任何指向 Profile 类的指针,只有在 Profile 类中。

有什么办法吗?

谢谢

【问题讨论】:

    标签: parse-platform parse-server


    【解决方案1】:

    您有多种选择:

    1) 您可以在执行LEFT JOINPerfil 类中使用aggregate 管道和$lookup 用户。但是,这不会返回Parse.Object 的数组,您必须手动解析结果。来自docs

    {
       $lookup:
         {
           from: <collection to join>,
           localField: <field from the input documents>,
           foreignField: <field from the documents of the "from" collection>,
           as: <output array field>
         }
    }
    

    2) 您可以通过首先获取所有用户然后通过用户 ID 获取他们的所有个人资料来执行 2 个请求。

    3) 您可以更改数据模型并在 User 类中添加指向 Perfil 的指针。如果您大规模运行此查询,它可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-07-03
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 2016-02-13
      相关资源
      最近更新 更多