【问题标题】:Filter entities on a sub-entity property in Google Datastore query在 Google 数据存储区查询中过滤子实体属性上的实体
【发布时间】:2016-05-18 05:06:29
【问题描述】:

我的数据存储区实体将具有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 的实体?

我像下图一样运行查询,但没有显示任何结果。

【问题讨论】:

  • 这是一个 Node.js sn-p

标签: google-cloud-datastore


【解决方案1】:

您可以通过用点连接属性名称并将该连接字符串用作查询中的属性名称来做到这一点。

在 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"
                }
              }
            }
          }
        }
      }
    }
  }
}

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

【讨论】:

  • 他们应该——如果他们不适合你,你能提供你尝试过的细节吗?
  • 我已经更新了问题,你能看看吗?
  • 我想你想要 zxc.nested.foobar=132(而不是 =32)?
  • zxc.nested.foobar=132 和 zxc.nested.foobar=32 都不起作用
  • 我无法在我自己的项目中重现此问题。您能否将您的项目 ID 和您希望查询返回的实体的密钥发送到 eddavisson@google.com,以便我仔细查看?
【解决方案2】:
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"

【讨论】:

  • 什么是query_facturas?它没有在您的示例代码中定义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 2012-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
相关资源
最近更新 更多