簡體   English   中英

Cloud Code(parse.com)中的關系查詢

[英]Relational queries in Cloud Code (parse.com)

表結構:

Collection
name (String)

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

Image類具有列collection ,該列collection是指向Collection類的指針。

position用於對Collection Images進行排序。


雲代碼中完成以下任務的最有效方法是什么?

假設我有〜3000張圖像和3個收藏集。

這將是構建一個查詢,返回包含具有至少一個相關聯的所有集合陣列的最佳方式Image ,並顯示僅在每個集合的第5幅圖像,通過排序position

每個集合的相關圖像都需要包含在響應中,並且可能看起來像這樣模糊:

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

  ...

}]    

目前,我能想到的就是執行兩個查詢,一個查詢獲取所有Collections ,另一個查詢所有Images ,然后對其進行過濾,這似乎是倒退的,再加上一個事實,即Parse最多限於1000個結果集。 我需要重新考慮我的表結構嗎?

使用該數據結構,您將有多個(嵌套的)查詢,但是在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

雲代碼:

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"
        }
      ]
    }
  ]
}

考慮到您的限制,您可以在一個調用中完成此操作。

如果您的Collection對象數量很少,並且使用的是相對位置(每個Collection的Image position = 1、2等),則無需考慮過多問題。 這些約束意味着'position'<6(如果您的第一個索引位置為0,則為5)相對較少。

只需對Image類進行調用,並將其限制在位置小於或等於5的對象上即可。使用.include()返回附加到每個Image對象的Collection對象,作為返回的一部分。 然后以您認為合適的格式在本地對Image對象進行排序。

請記住,解析限制將可以返回的對象數限制為100,並且您最多可以將其增加到1000。如果對每個ImageCollection使用.include() ,則意味着每個返回的Image計有2個對象,因此最多可以返回500個Image對象。 聽起來這遠遠超出了您的預期需求。

Voilá-一個電話,所有對象。

var Image = Parse.Object.extend("Image");
var query = new Parse.Query(Image);
query.lessThan("number", 6); //Assumes a lowest index of 1; switch to 5 if your lowest index number is 0
query.include("collection");
query.find({
  success: function(results) {
    //Sort the results according to Collection, send to controllers, etc.
    //Each result will have a pulled, valid Collection object ready on its .collection
  },
  error: function(error) {
    alert("Error: " + error.code + " " + error.message);
  }
});

更新還值得注意的是,無論是少量集合還是大規模(1000個或10,000個集合),此方法都優於“嵌套查詢”方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM