【问题标题】:Azure DocumentDB - Query returning no resultsAzure DocumentDB - 查询不返回任何结果
【发布时间】:2014-11-18 08:00:35
【问题描述】:

Azure DocumentDB 是否支持以下查询?它不返回任何文件。

Variables values at runtime:
  1. collectionLink = "<link for my collection>"
  2. feedOptions = new FeedOptions { MaxItemCount = 2 }
  3. name = "chris"

client.CreateDocumentQuery<T>(collectionLink, feedOptions).Where(m => (m.Status == "Foo" && (m.Black.Name == null || m.Black.Name != name) && (m.White.Name == null || m.White.Name != name)));

我已经使用更简单的查询进行了测试,如下所示,它们都返回了我期望的结果。

client.CreateDocumentQuery<T>(collectionLink, feedOptions).Where(m => m.Status == "Foo");

client.CreateDocumentQuery<T>(collectionLink, feedOptions).Where(m => m.Status == "Foo").Where(m => m.Size == 19);

最后,我确保有符合问题查询过滤条件的文档:

{
  "id": "1992db52-c9c6-4496-aaaa-f8cb83a8c6b0",
  "status": "Foo",
  "size": 19,
  "black": {
    "name": "charlie"
  },
  "white": {},
}

谢谢。

【问题讨论】:

    标签: azure azure-cosmosdb


    【解决方案1】:

    原来“m.White.Name == null || m.White.Name != name”检查存在问题,因为数据库中的文档中不存在 Name 字段。

    当文档被编辑为以下内容时,查询返回它。请注意 Name 字段的显式 null 值。

    {
      "id": "1992db52-c9c6-4496-aaaa-f8cb83a8c6b0",
      "status": "Foo",
      "size": 19,
      "black": {
        "name": "charlie"
      },
      "white": {
        "name": null
      },
    }
    

    【讨论】:

      【解决方案2】:

      可以编写查询以使用 DocumentDB UDF 处理缺失的属性,如下所示。 DocumentDB 使用 JavaScript 的语义,显式 null 与 JavaScript 中缺少的属性(“未定义”)不同。检查显式 null 很简单(== null 就像您的查询一样),但要查询 DocumentDB 中可能存在或不存在的字段,您必须首先为 ISDEFINED 创建一个 UDF:

      function ISDEFINED(doc, prop) {
          return doc[prop] !== undefined;   
      }
      

      然后在 DocumentDB 查询中使用它,例如:

      client.CreateDocumentQuery<T>(
          collectionLink, 
         "SELECT * FROM docs m WHERE m.Status == "Foo" AND (ISDEFINED(m.white, "name") OR m.white.name != name)");
      

      希望这会有所帮助。请注意,由于 != 和 UDF 都需要扫描,因此对于性能/规模而言,始终只在具有其他过滤器的查询中使用它们是一个好主意。

      【讨论】:

      • 我现在将IS_DEFINED(expr) 视为标准类型检查功能。是在您编写上述内容和现在之间添加的,还是我没有看到您的 UDF 和 IS_DEFINED 有什么不同?
      猜你喜欢
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多