检索部分数组属性

时间:2018-06-25 14:12:45

标签: json azure azure-cosmosdb

我们正在存储这样的文档:

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

通常,当我们检索“ Jim”记录时,我们希望获得有关他和他的孩子们的完整详细信息,但是在某些情况下,我们将只需要他的名字,以及每个孩子的名字和他们每个孩子的头衔。爱好。

在保留(或即时重建)其结构的同时,是否有一种直接(或没有)方法来检索这些文档的一部分?

如果我尝试类似的操作:

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

即使我不得不丢失结构,我仍然想取回一个代表“ Jim”的记录,其中包含他孩子的名字和爱好,但是现在我只是检索所有内容并在客户端,这不理想。

我有可能吗?

1 个答案:

答案 0 :(得分:1)

根据您的情况,建议您使用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"
                    }
                ]
            }
        ]
    }
]

任何担心,请随时让我知道。