如何根据内部 json 对象值在 DocumentDB 中查询?

How to query in DocumentDB based on inner json object value?

假设我在 DocumentDB 中有 3 个这样的对象。

这是 class 条记录。

现在我想获取姓名为 sunny 的学生所在的所有 ID。

{
  "id": "111",
  "class": 1,
  "students": [
    {
      "name": "sunny"
    },
    {
      "name": "pinki"
    },
    {
      "name": "bobby"
    },
    {
      "name": "lucky"
    }
  ]
}

{
  "id": "222",
  "class": 2,
  "students": [
    {
      "name": "pinki"
    },
    {
      "name": "sunny"
    },
    {
      "name": "bobby"
    }
  ]
}

{
  "id": "333",
  "class": 3,
  "students": [
    {
      "name": "pinki"
    },
    {
      "name": "lucky"
    },
    {
      "name": "bobby"
    }
  ]
}

得到结果的查询是什么?

您可以使用 DocumentDB 的 JOIN 在具有数组元素的文档上创建叉积。

例如,以下查询在 students 属性 的文档上创建叉积以在 students.name 上查询:

select doc.id
from doc
join students in doc.students
where students.name = 'sunny'

returns 以下数据集:

[{
    id: 111
}, {
    id: 222
}]

参考:http://azure.microsoft.com/en-us/documentation/articles/documentdb-sql-query/#joins