解析服务器--获取相关类--休息api。

问题描述 投票:0回答:1

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

profile类有一个叫做 "sex "的字段和一个叫做 "user "的字段,这是一个指向用户类的指针。

如果我得到的profile端点是.https:/myapi.back4app.ioclassesProfile。https:/myapi.back4app.ioclassesProfile。 我可以得到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"
        }
    ]
}

如果我想包含这个profile的用户,我可以用以下方式包含: https:/myapi.back4app.ioclassesPerfil?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个回答
0
投票

你有几个选择。

1) 你可以使用一个 aggregate 管道和 $lookup 中的用户 Perfil 类,执行 LEFT JOIN. 然而,这将不会返回一个由 Parse.Object你必须手动解析结果。从 文件:

{
   $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)你可以做2个请求,先得到所有的用户,然后通过用户ID得到他们的所有资料。

3) 你可以改变你的数据模型,添加一个指针到 Perfil 在你 User 类。如果你在大规模运行这个查询,可能会有好处。

© www.soinside.com 2019 - 2024. All rights reserved.