在 Google 数据存储区查询中过滤子实体 属性 上的实体

Filter entities on a sub-entity property in Google Datastore query

我的数据存储实体将具有 embedded entity type 的属性。

我将它们保存如下(我正在使用 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); });

有什么方法可以查询具有 zxc.nested.foobar=132 的实体吗?

我运行如下图查询,没有显示结果。

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

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

{
  "query": 
  {
    "kinds": 
    [
      {
        "name": "MyKind"
      }
    ],
    "filter": 
    {
      "propertyFilter": 
      {
        "property": 
        {
          "name": "zxc.nested.foobar"
        },
        "operator": "EQUAL",
        "value": 
        {
          "integerValue": "132"
        }
      }
    }
  }
}

请注意,为了显示结果,必须为每个属性编制索引。在 JSON API:

中默认是这样的
{
  "key": 
  {
    "path": 
    [
      {
        "kind": "MyKind",
        "id": 1
      }
    ]
  },
  "properties": 
  {
    "zxy": 
    {
      "entityValue": 
      {
        "properties": 
        {
          "nested": 
          {
            "entityValue": 
            {
              "properties": 
              {
                "foobar": 
                {
                  "integerValue": "132"
                }
              }
            }
          }
        }
      }
    }
  }
}

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

const Datastore = require('@google-cloud/datastore');
// Your Google Cloud Platform project ID
const projectId = 'your-project-id';
// Creates a client
const datastore = new Datastore({
  projectId: projectId,
});
let query = datastore.createQuery('MyKind');
let query = query.filter('xyz.foobar', '=', 32);
query.run()
  .then( (results) => {
    //do your thing
  });

我知道我来晚了,但如果有人需要它,就在这里。它适用于 "@google-cloud/datastore": "^1.3.4"