Cloud Code中的关系查询(parse.com)

时间:2015-04-28 17:32:46

标签: javascript parse-platform

表格结构:

Collection
name (String)

Image
collection (Pointer<Collection>)
url (String)
position (Number)

Image类具有列collection,它是指向Collection类的指针。

position用于对Images内的Collection进行排序。

Cloud Code中最有效的方法是什么?

假设我有~3000张图片和3个收藏品。

构建一个返回数组的查询的最佳方法是什么,该数组包含至少一个关联Image且仅显示 的所有集合每个集合的5张图片,按position

排序

每个集合的相关图像都需要包含在响应中,并且可能看起来像这样:

results: [{
  collection: {
    name: 'foo'
  },
  images: [{
    position: 0,
    url: 'test.jpg'
  },
  {
    position: 1,
    url: 'test.gif'
  }]
}, {

  ...

}]    

目前我所能想到的只是做两个查询,一个是获取所有Collections而另一个是预先获得所有Images然后过滤它们,这看起来相当倒退,再加上事实Parse仅限于最大1000的结果集。我是否需要重新考虑我的桌面结构?

2 个答案:

答案 0 :(得分:1)

使用该数据结构,您将拥有多个(嵌套)查询,但在CloudCode中完成时,如果使用适当的限制和承诺,它仍应足够快。

在这里尝试卷曲:

curl -X POST   -H "X-Parse-Application-Id: 1ClVjWioWtW4uz8Nt7zYiXHkoNYQao6Z1rdXd8Gt"   -H "X-Parse-REST-API-Key: VlYbFT8uhPpsYeRW5ezIn1A8bWa5N9OW9riqs4ji"   -H "Content-Type: application/json"   -d '{}'   https://api.parse.com/1/functions/collectionSummary

Cloud Code:

var _ = require('underscore');

Parse.Cloud.define("collectionSummary", function(request, response) {
    var collections = [];
    var collectionQuery = new Parse.Query("Collection");
    collectionQuery.find({
        success: function(results) {
            var promises = [];
            _.each(results, function(collection) {
                var name = collection.get("name");
                var imageQuery = new Parse.Query("Image");
                imageQuery.equalTo("collection", collection);
                imageQuery.ascending("position");
                imageQuery.limit(5);
                promises.push(imageQuery.find({
                    success: function(images) {
                        if (images.length > 0) {
                            collections.push({ "collection":collection, "images":images });
                        }
                    },
                    error: function() {
                        response.error("Could not read images.");
                    }
                }));
            });
            Parse.Promise.when(promises).then(function() {
                response.success(collections);
            });
        },
        error: function() {
            response.error("Could not read collections.");
        }
    });
});

输出:

{
  "result": [
    {
      "collection": {
        "__type": "Object",
        "className": "Collection",
        "createdAt": "2015-05-11T21:52:28.406Z",
        "name": "Apple",
        "objectId": "cpTsJBNc9q",
        "updatedAt": "2015-05-11T21:52:28.406Z"
      },
      "images": [
        {
          "__type": "Object",
          "className": "Image",
          "collection": {
            "__type": "Pointer",
            "className": "Collection",
            "objectId": "cpTsJBNc9q"
          },
          "createdAt": "2015-05-11T22:12:35.152Z",
          "objectId": "0AmtlLrlHT",
          "position": 0,
          "updatedAt": "2015-05-11T22:12:49.463Z",
          "url": "test0"
        },
        {
          "__type": "Object",
          "className": "Image",
          "collection": {
            "__type": "Pointer",
            "className": "Collection",
            "objectId": "cpTsJBNc9q"
          },
          "createdAt": "2015-05-11T22:12:58.185Z",
          "objectId": "WbomPr3hUK",
          "position": 1,
          "updatedAt": "2015-05-11T22:13:05.523Z",
          "url": "test1"
        },
        {
          "__type": "Object",
          "className": "Image",
          "collection": {
            "__type": "Pointer",
            "className": "Collection",
            "objectId": "cpTsJBNc9q"
          },
          "createdAt": "2015-05-11T22:19:02.836Z",
          "objectId": "ASLInM7Hu5",
          "position": 15,
          "updatedAt": "2015-05-11T22:19:08.990Z",
          "url": "test8"
        },
        {
          "__type": "Object",
          "className": "Image",
          "collection": {
            "__type": "Pointer",
            "className": "Collection",
            "objectId": "cpTsJBNc9q"
          },
          "createdAt": "2015-05-11T22:19:14.719Z",
          "objectId": "VkCF98Ts0N",
          "position": 20,
          "updatedAt": "2015-05-11T22:19:20.699Z",
          "url": "test9"
        },
        {
          "__type": "Object",
          "className": "Image",
          "collection": {
            "__type": "Pointer",
            "className": "Collection",
            "objectId": "cpTsJBNc9q"
          },
          "createdAt": "2015-05-11T22:19:27.032Z",
          "objectId": "I2mEG20bfp",
          "position": 40,
          "updatedAt": "2015-05-11T22:19:37.554Z",
          "url": "test10"
        }
      ]
    },
    {
      "collection": {
        "__type": "Object",
        "className": "Collection",
        "createdAt": "2015-05-11T21:52:35.297Z",
        "name": "Banana",
        "objectId": "zxPfnWlm1T",
        "updatedAt": "2015-05-11T21:52:35.297Z"
      },
      "images": [
        {
          "__type": "Object",
          "className": "Image",
          "collection": {
            "__type": "Pointer",
            "className": "Collection",
            "objectId": "zxPfnWlm1T"
          },
          "createdAt": "2015-05-11T22:16:05.924Z",
          "objectId": "vK9O7b5vR6",
          "position": 0,
          "updatedAt": "2015-05-11T22:16:12.607Z",
          "url": "test2"
        },
        {
          "__type": "Object",
          "className": "Image",
          "collection": {
            "__type": "Pointer",
            "className": "Collection",
            "objectId": "zxPfnWlm1T"
          },
          "createdAt": "2015-05-11T22:16:26.676Z",
          "objectId": "oMEMgwauEi",
          "position": 1,
          "updatedAt": "2015-05-11T22:16:30.750Z",
          "url": "test3"
        },
        {
          "__type": "Object",
          "className": "Image",
          "collection": {
            "__type": "Pointer",
            "className": "Collection",
            "objectId": "zxPfnWlm1T"
          },
          "createdAt": "2015-05-11T22:16:35.378Z",
          "objectId": "0sAbNK1LbN",
          "position": 2,
          "updatedAt": "2015-05-11T22:16:39.475Z",
          "url": "test4"
        },
        {
          "__type": "Object",
          "className": "Image",
          "collection": {
            "__type": "Pointer",
            "className": "Collection",
            "objectId": "zxPfnWlm1T"
          },
          "createdAt": "2015-05-11T22:16:42.984Z",
          "objectId": "QVZJmHQJ6E",
          "position": 3,
          "updatedAt": "2015-05-11T22:16:52.405Z",
          "url": "test5"
        },
        {
          "__type": "Object",
          "className": "Image",
          "collection": {
            "__type": "Pointer",
            "className": "Collection",
            "objectId": "zxPfnWlm1T"
          },
          "createdAt": "2015-05-11T22:16:56.827Z",
          "objectId": "NGS39LCAJL",
          "position": 4,
          "updatedAt": "2015-05-11T22:17:01.401Z",
          "url": "test6"
        }
      ]
    }
  ]
}

答案 1 :(得分:1)

根据您的约束,您可以在一次通话中执行此操作。

如果您有少量Collection个对象并且您正在使用相对位置(每个Collection都有一个图像position = 1,2等),那么无需过度思考问题。这些约束意味着“位置”<1的项目相对较少。 6(或5,如果0是你的第一个指数位置)。

只需在Image类上调用并将其限制为位置小于或等于5的对象。使用.include()返回附加到每个Collection的对象{1}}对象作为返回的一部分。然后以您认为合适的任何格式在本地对Image个对象进行排序。

请记住,Parse限制的对象数量可以返回到100,并且您可以将其增加到最多1000个。如果Image.include()每个Collection,它表示每个返回的Image计算2个对象,因此最多可以返回500个Image个对象。听起来这远远超出了你的预期需求。

Voilá - 一个电话,你的所有物品。

Image

<强>更新 值得注意的是,这种方法优于“嵌套查询”方法,无论是少量集合还是大规模集合(1000或10,000集合)