在 Google Datastore 查询中过滤子实体属性上的实体

Evg*_*nko 4 google-cloud-datastore

我的数据存储实体将具有嵌入式实体类型的属性。

在我将它们保存如下后(我使用的是 gcloud v0.27.0):

dataset.save([{
    key: dataset.key({ path: ['MyKind', 1] }),
        data: {
            foo: 'bar',
            zxc: {
                nested: {
                    foobar: 32
                }
            }
        }
    }, 
    {
        key: dataset.key({ path: ['MyKind', 2] }),
        data: {
            foo: 'a string',
            zxc: {
                nested: {
                    foobar: 132
                }
            }
        }
    }
    ], function(error) { console.log(error); });
Run Code Online (Sandbox Code Playgroud)

有没有办法查询实体,比如说,有zxc.nested.foobar=132

我运行如下图所示的查询,但没有显示任何结果。

在此处输入图片说明

Ed *_*son 5

您可以通过用点连接属性名称并使用连接的字符串作为查询中的属性名称来实现这一点。

在 Cloud Datastore v1beta3 API 中,JSON 请求如下所示:

{
  "query": 
  {
    "kinds": 
    [
      {
        "name": "MyKind"
      }
    ],
    "filter": 
    {
      "propertyFilter": 
      {
        "property": 
        {
          "name": "zxc.nested.foobar"
        },
        "operator": "EQUAL",
        "value": 
        {
          "integerValue": "132"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,为了显示结果,必须对每个属性进行索引。这是 JSON API 中的默认情况:

{
  "key": 
  {
    "path": 
    [
      {
        "kind": "MyKind",
        "id": 1
      }
    ]
  },
  "properties": 
  {
    "zxy": 
    {
      "entityValue": 
      {
        "properties": 
        {
          "nested": 
          {
            "entityValue": 
            {
              "properties": 
              {
                "foobar": 
                {
                  "integerValue": "132"
                }
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

默认情况下,数据存储客户端库通常也会索引属性,但某些旧版本gcloud-node(例如 0.27.0)可能不会。