这个结果的SQL字符串?

时间:2019-02-05 00:29:28

标签: azure-cosmosdb-sqlapi

我想对集合中的文档进行简单的转换。我想改变这个:

{
  "dossier": {
    "firstName": "Mortimer",
    "lastName": "Snurd",
    "title": "Manager",
    "company": "MyCompany"
  },
  "events": {
    "eventA": {
       "questions": [
        {
          "id": 123,
          "answer": "hello"
        },
        {
          "id": 456,
          "answer": "world"
        }
      ]
    }
  },
  "id": "1d7070f0-a00b-46e8-87ee-67ba8e192639"
}

对此:

{
  "dossier": {
    "firstName": "Mortimer",
    "lastName": "Snurd",
  },
  "events": {
    "eventA": {
       "questions": [
        {
          "id": 123
        },
        {
          "id": 456
        }
      ]
    }
  },
  "id": "1d7070f0-a00b-46e8-87ee-67ba8e192639"
}

我只是想从档案中删除“标题”和“公司”,并从events.eventA.questions []中删除“答案”。

我认为这应该很简单,但我一直无法弄清楚。到目前为止,我已经提出了这个查询

SELECT {"firstName": c.dossier.firstName, "lastName": c.dossier.lastName} AS dossier, 
{
  "questions": {"id": q.id}
} as eventA

from c
join q in c.events.eventA.questions
WHERE c.id = '1d7070f0-a00b-46e8-87ee-67ba8e192639'

但这给了我错误的结果;

[
    {
        "dossier": {
            "firstName": "Mortimer",
            "lastName": "Snurd"
        },
        "eventA": {
            "questions": {
                "id": 123
            }
        }
    },
    {
        "dossier": {
            "firstName": "Mortimer",
            "lastName": "Snurd"
        },
        "eventA": {
            "questions": {
                "id": 456
            }
        }
    }
]

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我发现ARRAY函数后就知道了。最终的SQL是

SELECT {"firstName": c.dossier.firstName, "lastName": c.dossier.lastName} AS dossier, 
{
  "questions": ARRAY(SELECT q.id from  q in c.events.eventA.questions)
} as eventA

from c
WHERE c.id = '1d7070f0-a00b-46e8-87ee-67ba8e192639'

我知道这很简单。 :)