代码之家  ›  专栏  ›  技术社区  ›  Sven Grosen

检索部分数组属性

  •  2
  • Sven Grosen  · 技术社区  · 7 年前

    {
       "FullName": "Jim",
       "Children": [
         {
            "Name": "Sue",
            "Hobbies": [
               {
                   "Title": "Stamps",
                   "EnthusiasmLevel": 1
               },
               {
                   "Title": "Baseball",
                   "EnthusiasmLevel": 5
               }
            ]
         },
         {
            "Name": "Frank",
            "Hobbies": [
               {
                   "Title": "Dance",
                   "EnthusiasmLevel": 3
               },
               {
                   "Title": "Juggling",
                   "EnthusiasmLevel": 2
               }
            ]
         }
       ]
    }
    

    通常,当我们检索这个“吉姆”的记录时,我们会想要他和他的孩子的详细信息,但在某些情况下,我们会想要他的名字,每个孩子的名字和他们每个爱好的标题。

    如果我尝试以下方法:

    SELECT p.FullName, [{"Name": child.Name}] AS Children
    FROM People AS p
    JOIN child in p.Children
    

    我想得到的是:

    {
      "FullName": "Jim",
      "Children": [
         { 
            "Name": "Sue",
            "Hobbies": [
              {"Title": "Stamps"},
              {"Title": "Baseball"}
            ]
         },
         { 
            "Name": "Frank",
            "Hobbies": [
              {"Title": "Dance"},
              {"Title": "Juggling"}
            ]
         }
      ]
    }
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jay Gong    7 年前

    根据您的情况,我建议您使用 Stored Procedure

    样本代码:

    function sample() {
        var collection = getContext().getCollection();
    
        var isAccepted = collection.queryDocuments(
            collection.getSelfLink(),
            'SELECT p.FullName, p.Children FROM People AS p',
        function (err, feed, options) {
            if (err) throw err;
            if (!feed || !feed.length) {
                var response = getContext().getResponse();
                response.setBody('no docs found');
            }
            else {
                var response = getContext().getResponse();
                var returnResult = [];
                for(var i = 0;i<feed.length;i++){
                    var peopleObj = feed[i];
                    ChildrenArray = [];               
                    for(var j = 0;j<peopleObj.Children.length;j++){
                        console.log(j)
                        var childObj = peopleObj.Children[j];
                        HobbiesArray = [];
                        for(var k = 0; k < childObj.Hobbies.length;k++){
                            var hobbyObj = childObj.Hobbies[k];
                            map ={};
                            map["Title"] = hobbyObj.Title;
                            HobbiesArray.push(map);
                        }
                        childObj.Hobbies = HobbiesArray;
                    }
                    ChildrenArray.push(childObj);
                }
                returnResult.push(peopleObj);
                getContext().getResponse().setBody(returnResult);
            }
        });
    
        if (!isAccepted) throw new Error('The query was not accepted by the server.');
    }
    

    [
        {
            "FullName": "Jim",
            "Children": [
                {
                    "Name": "Sue",
                    "Hobbies": [
                        {
                            "Title": "Stamps"
                        },
                        {
                            "Title": "Baseball"
                        }
                    ]
                },
                {
                    "Name": "Frank",
                    "Hobbies": [
                        {
                            "Title": "Dance"
                        },
                        {
                            "Title": "Juggling"
                        }
                    ]
                }
            ]
        }
    ]