放松在DocumentDB查询
数组我有一个看起来是这样的文件:放松在DocumentDB查询
[ {
"id": "e1bb9b05-11f2-459e-37d3-9bf9fed56c96",
"name": "bulbasaur",
"type": [
{
"slot": 2,
"type": {
"url": "https://pokeapi.co/api/v2/type/4/",
"name": "poison"
}
},
{
"slot": 1,
"type": {
"url": "https://pokeapi.co/api/v2/type/12/",
"name": "grass"
}
}
]
}
]
下面的查询是最接近我能得到的,但不太我希望的输出。
查询
SELECT c.id, c.name, t.type.name as type
FROM
c
JOIN
t IN c.types
WHERE
c.name = "bulbasaur"
结果
[ {
"id": "e1bb9b05-11f2-459e-37d3-9bf9fed56c96",
"name": "bulbasaur",
"type": "poison"
},
{
"id": "e1bb9b05-11f2-459e-37d3-9bf9fed56c96",
"name": "bulbasaur",
"type": "grass"
}
]
盼望
[ {
"id": "e1bb9b05-11f2-459e-37d3-9bf9fed56c96",
"name": "bulbasaur",
"types": ["poison", "grass"]
}
]
这可能与文档数据库查询?
回答:
这需要使用DocumentDB UDFs,它可以通过自定义转换扩展查询功能。例如,注册这个:
function unwindTypeArray(value) { var result = { id: value.id, name: value.name, types: []};
for (var idx in value.type) {
console.log(idx);
var name = value.type[idx].type.name;
result.types.push(name);
}
return result;
}
然后调用它像一个查询中:
SELECT udf.unwindTypeArray(c) FROM c WHERE c.name = "bulbasaur"
以上是 放松在DocumentDB查询 的全部内容, 来源链接: utcz.com/qa/260489.html