鉴于这两个文件版本:
{
"id": "1",
"tags": ["first","second"],
"version": 1,
}
和
{
"id": "2",
"tags": [
{"name": "third"},
{"name": "forth"}
],
"version": 2,
}
我第一次尝试将它们标准化为阅读:
SELECT VALUE t
FROM c
JOIN t IN c.tags
WHERE c.version = 1
UNION ALL
SELECT VALUE t.name
FROM c
JOIN t IN c.tags
WHERE c.version = 2
看起来不支持UNION。那么处理版本控制的最佳方法是什么?如果可能的话,我希望使用单个聚合查询以统一的方式处理所有版本。
答案 0 :(得分:1)
我建议你在cosmos db中使用存储过程。请参阅我的示例代码:
function sample() {
var collection = getContext().getCollection();
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT c.version,c.tags FROM root c',
function (err, feed, options) {
if (err) throw err;
if (!feed || !feed.length) getContext().getResponse().setBody('no docs found');
else {
var returnResult = [];
for(var i = 0;i<feed.length;i++){
var tagArray = feed[i].tags;
if(tagArray[0].name == null){
for(var j = 0;j<tagArray.length;j++)
returnResult.push(tagArray[j])
}else {
for(var j = 0;j<tagArray.length;j++)
returnResult.push(tagArray[j].name)
}
}
getContext().getResponse().setBody(returnResult);
}
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
我的示例文档:
[
{
"id": "1",
"tags": [
"first",
"second"
],
"version": 1
},
{
"id": "2",
"tags": [
{
"name": "third"
},
{
"name": "forth"
}
],
"version": 2
},
{
"id": "3",
"tags": [
{
"name": "third"
},
{
"name": "forth"
}
],
"version": 3
},
{
"id": "4",
"tags": [
"first",
"second"
],
"version": 4
}
]
执行结果:
希望它对你有所帮助。
答案 1 :(得分:0)
有效:
SELECT VALUE c.version = 1 ? t : t.name
FROM c
JOIN t IN c.tags
但是支持多个版本会很困难。我更愿意独立处理所有版本预测...