DocumentDb 无法处理列名中的连字符 (-)

DocumentDb cannot handle hyphen (-) in column names

我将以下 XML 保存到 DocumentDB:

<DocumentDbTest_Countries> 
  <country>C25103657983</country> 
  <language>C25103657983</language> 
  <countryCode>383388823</countryCode>
  <version>2015-08-25T08:36:59:982.3552</version>
  <integrity>
     <hash-algorithm>sha1</hash-algorithm>
     <hash /> 
  </integrity>
  <context-info> 
     <created-by>unittestuser</created-by> 
     <created-on>2015/08/25 08:36:59</created-on> 
     <created-time-zone>UTC</created-time-zone> 
     <modified-by>unittestuser</modified-by>
     <modified-on>2015/08/25 08:36:59</modified-on> 
     <modified-time-zone>UTC</modified-time-zone>
  </context-info>
</DocumentDbTest_Countries>

保存到 DocumentDB 中的内容如下:

{
"DocumentDbTest_Countries": {
"integrity": {
   "hash-algorithm": "sha1",
   "hash": ""
  },
"context-info": {
  "created-by": "unittestuser",
  "created-on": "2015/08/25 08:36:59",
  "created-time-zone": "UTC",
  "modified-by": "unittestuser",
  "modified-on": "2015/08/25 08:36:59",
  "modified-time-zone": "UTC"
},
"country": "C25103657983",
"language": "C25103657983",
"countryCode": 383388823,
"version": "2015-08-25T08:36:59:982.3552"
},
"id": "f917945d-eaee-4eff-944d-dae366de7be1"
}

如您所见,列名确实在 DocumentDB 中保存了连字符 (-)(显然没有任何类型的 errors/exceptions/warning),但是当我尝试进行查找时,它在查询浏览器。似乎无法搜索带连字符的列名。这是真的?或者,我错过了什么吗?有人可以指点我在某处关于此限制的文档吗?

You can also access properties using the quoted property operator []. For example, SELECT c.grade and SELECT c["grade"] are equivalent. This syntax is useful when you need to escape a property that contains spaces, special characters, or happens to share the same name as a SQL keyword or reserved word.

- 是这些特殊字符之一,因此要访问包含 - 的 属性,您需要使用带引号的 属性 运算符。 已记录:)

当然,惯用的方法是使用驼峰式大小写而不是连字符,但如果您不想更改结构,则需要使用引用的属性。

例如,使用您的测试数据,此查询有效:

SELECT c["country-code"] FROM root.DocumentDbTest_Countries c

编辑:

查询的语法有点混乱,这是导致您出现大部分问题的原因。与您的想法相反,

select * from DocumentDbTest_Countries

实际上不是 "get me all the data in DocumentDbTest_Countries"。相反,它似乎意味着 "get me all the data in the current collection, and alias it as DocumentDbTest_Countries"。当您查看 returned 数据时,这一点很明显 - 您希望它 return 仅 DocumentDbTest_Countries 内的字段,但实际上 return 是所有值,包括 id 不是 的一部分 DocumentDbTest_Countries - 早先应该很明显 :D)。

我不明白为什么要这样设计(即使使用DocumentDbTest_Countries c明确指定别名也不会selectDocumentDbTest_Countries),但修复是实际开始带有集合名称的标识符。 root只是引用"this collection"的一种方式,所以

select * from root.DocumentDbTest_Countries

return是您对原始查询的期望。除非您弄清楚原始查询的行为方式为何如此,否则我会坚持每次都明确使用 root(或集合名称)作为根。在我看来,使用 from whatever 将始终 return 当前集合,除非你有一个名为 whatever 的集合——如果你问我,这是一个奇怪的设计决定。这意味着除非你有一个名为 lotsOfFun 的集合,否则下面的工作方式与使用 root:

相同
select * from lotsOfFun.DocumentDbTest_Countries

可能是因为顶级对象没有命名,所以他们决定随便起什么名字都行,但这只是一个想法。

对于使用特定字符(space、“@”、“-”等)或与 SQL 关键字冲突的字段名称,您必须使用带引号的 属性 访问器语法。所以不要写:

SELECT * FROM c WHERE c.context-info.created-by = "unittestuser"

写入:

SELECT * FROM c WHERE c["context-info"]["created-by"] = "unittestuser"

好吧,诀窍是使用 CollectionName.DocumentName 而不仅仅是 DocumentName,就像这样(感谢@Laan指着我那个方向):):

SELECT * FROM TestProject.DocumentDbTest_Countries c where c["@country"] = "C26092630539"

但是我还是想念 return 文档数据中的 Document.Id 和 Document.SelfLink 数据。