Return 只有 child 条件的一个子集
Return only a subset of child from condition
是否可以对 children 进行条件查询?我正在尝试 return 所有组,所有子组(还有 children),其中计数大于 0。请注意,子组有 children 也需要 returned 如果计数大于 0。
文件:
{
"id": 1,
"name": "name1",
"groups": [
{
"id": 1,
"name": "name1",
"subGroups": [
{
"id": 1,
"name": "name1",
"count": 4,
"assests": [ "asset1", "asset2" ]
},
{
"id": 2,
"name": "name2",
"count": 0,
"assests": [ "asset1", "asset2" ]
}
]
},
{
"id": 2,
"name": "name2",
"subGroups": [
{
"id": 1,
"name": "name1",
"count": 4,
"assests": [ "asset1", "asset2" ]
},
{
"id": 2,
"name": "name2",
"count": 0,
"assests": [ "asset1", "asset2" ]
}
]
}
]
}
想要的结果:
{
"id": 1,
"name": "name1",
"groups": [
{
"id": 1,
"name": "name1",
"subGroups": [
{
"id": 1,
"name": "name1",
"count": 4,
"assests": [ "asset1", "asset2" ]
}
]
},
{
"id": 2,
"name": "name2",
"subGroups": [
{
"id": 1,
"name": "name1",
"count": 2,
"assests": [ "asset1", "asset2" ]
}
]
}
]
}
我尝试通过加入进行操作,但没有找到可以跳过某些子组而不是其他子组的方法。欢迎所有建议。
我建议您使用 stored procedure 来实现您想要的结果。请参考我的示例代码:
function sample() {
var collection = getContext().getCollection();
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM root r',
function (err, feed, options) {
if (err) throw err;
if (!feed || !feed.length) {
var response = getContext().getResponse();
response.setBody('no docs found');
}
else {
var returnArray = [];
for(var i=0 ;i<feed.length;i++){
var groupsArray = feed[i].groups;
var map ={};
var groups = [];
for(var j=0 ;j<groupsArray.length; j++){
var map1 = {};
map1["id"] = groupsArray[j].id;
map1["name"] = groupsArray[j].name;
var subGroupsArray = groupsArray[j].subGroups;
var sub = [];
for(var k=0 ;k<subGroupsArray.length;k++){
if(subGroupsArray[k].count > 0)
sub.push(subGroupsArray[k]);
}
map1["subGroups"] = sub;
groups.push(map1);
}
map["id"] = feed[i].id;
map["name"] = feed[i].name;
map["groups"] = groups;
returnArray.push(map);
}
var response = getContext().getResponse();
response.setBody(returnArray);
}
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
好吧,也许您可以在 SQL 查询中使用 UDF。但是,您仍然需要在 udf 函数中循环数组。
function test(groupsArray){
var map ={};
var groups = [];
for(var j=0 ;j<groupsArray.length; j++){
var map1 = {};
map1["id"] = groupsArray[j].id;
map1["name"] = groupsArray[j].name;
var subGroupsArray = groupsArray[j].subGroups;
var sub = [];
for(var k=0 ;k<subGroupsArray.length;k++){
if(subGroupsArray[k].count > 0)
sub.push(subGroupsArray[k]);
}
map1["subGroups"] = sub;
groups.push(map1);
}
return groups;
}
sql:
SELECT c.id,c.name,udf.test(c.groups) as groups FROM c
是否可以对 children 进行条件查询?我正在尝试 return 所有组,所有子组(还有 children),其中计数大于 0。请注意,子组有 children 也需要 returned 如果计数大于 0。
文件:
{
"id": 1,
"name": "name1",
"groups": [
{
"id": 1,
"name": "name1",
"subGroups": [
{
"id": 1,
"name": "name1",
"count": 4,
"assests": [ "asset1", "asset2" ]
},
{
"id": 2,
"name": "name2",
"count": 0,
"assests": [ "asset1", "asset2" ]
}
]
},
{
"id": 2,
"name": "name2",
"subGroups": [
{
"id": 1,
"name": "name1",
"count": 4,
"assests": [ "asset1", "asset2" ]
},
{
"id": 2,
"name": "name2",
"count": 0,
"assests": [ "asset1", "asset2" ]
}
]
}
]
}
想要的结果:
{
"id": 1,
"name": "name1",
"groups": [
{
"id": 1,
"name": "name1",
"subGroups": [
{
"id": 1,
"name": "name1",
"count": 4,
"assests": [ "asset1", "asset2" ]
}
]
},
{
"id": 2,
"name": "name2",
"subGroups": [
{
"id": 1,
"name": "name1",
"count": 2,
"assests": [ "asset1", "asset2" ]
}
]
}
]
}
我尝试通过加入进行操作,但没有找到可以跳过某些子组而不是其他子组的方法。欢迎所有建议。
我建议您使用 stored procedure 来实现您想要的结果。请参考我的示例代码:
function sample() {
var collection = getContext().getCollection();
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM root r',
function (err, feed, options) {
if (err) throw err;
if (!feed || !feed.length) {
var response = getContext().getResponse();
response.setBody('no docs found');
}
else {
var returnArray = [];
for(var i=0 ;i<feed.length;i++){
var groupsArray = feed[i].groups;
var map ={};
var groups = [];
for(var j=0 ;j<groupsArray.length; j++){
var map1 = {};
map1["id"] = groupsArray[j].id;
map1["name"] = groupsArray[j].name;
var subGroupsArray = groupsArray[j].subGroups;
var sub = [];
for(var k=0 ;k<subGroupsArray.length;k++){
if(subGroupsArray[k].count > 0)
sub.push(subGroupsArray[k]);
}
map1["subGroups"] = sub;
groups.push(map1);
}
map["id"] = feed[i].id;
map["name"] = feed[i].name;
map["groups"] = groups;
returnArray.push(map);
}
var response = getContext().getResponse();
response.setBody(returnArray);
}
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
好吧,也许您可以在 SQL 查询中使用 UDF。但是,您仍然需要在 udf 函数中循环数组。
function test(groupsArray){
var map ={};
var groups = [];
for(var j=0 ;j<groupsArray.length; j++){
var map1 = {};
map1["id"] = groupsArray[j].id;
map1["name"] = groupsArray[j].name;
var subGroupsArray = groupsArray[j].subGroups;
var sub = [];
for(var k=0 ;k<subGroupsArray.length;k++){
if(subGroupsArray[k].count > 0)
sub.push(subGroupsArray[k]);
}
map1["subGroups"] = sub;
groups.push(map1);
}
return groups;
}
sql:
SELECT c.id,c.name,udf.test(c.groups) as groups FROM c