简体   繁体   English

如何在Google Cloud Datastore(Node.js)中执行“仅键查询”

[英]How to do a “keys-only-query” in Google Cloud Datastore (Node.js)

I am trying to do a "keys-only query" with Google Datastore API for Node.js, as exemplified in the documentation here . 我试图做一个“纯键查询”与谷歌的数据存储API对Node.js的,如文档中的例子在这里

I do that after having saved a number of records like this: 保存了许多这样的记录后,我便这样做了:

datastore.save(
  records.map(
    (record) => {
      return {
        key: datastore.key([kind, record.id]),
        data: record,
      };
    }
  )

The constant kind is a string. 常量kind是一个字符串。 Each record has a valid unique id property (a number), which, as shown here, should also serve as the datastore key identifier. 每个record都有一个有效的唯一id属性(一个数字),如此处所示,它也应作为数据存储区密钥标识符。

The records are stored correctly. 记录已正确存储。 I can retrieve them all without problems through 我可以通过没有问题的方式全部检索它们

datastore.runQuery(datastore.createQuery(kind))
.then( (results) => {
    // whatever...
}

All the saved records are returned correctly. 所有保存的记录均正确返回。

But when I do a "keys-only query" like this (and as exemplified in the documentation ): 但是,当我执行这样的“仅键查询”(并在文档中举例说明 )时:

const query = datastore.createQuery(kind)
  .select('__key__');
datastore.runQuery(query)
.then( (results) => {
    // whatever...
}

my results[0] return value is simply an array of empty objects like this: 我的results[0]返回值只是一个空对象数组,如下所示:

results[0]: [ {}, {}, {}, {}, {}, ..., {}]

The number of empty objects returned here is the correct number of records of the given kind. 此处返回的空对象数是给定类型的正确记录数。 But the problem is that they are empty objects. 但是问题在于它们是空对象。 I expected to get the datastore key for each record here. 我希望在这里获得每个记录的数据存储区密钥。

If, on the other hand, I do a "normal" projection query, on a "normal" property (like "id" - which should be identical with the datastore key, as far as I understand, after having defined the key through datastore.key[kind, record.id] ), I retrieve the projected "id" properties correctly thus: 另一方面,如果我在通过datastore.key[kind, record.id]定义键之后,对“正常”属性(例如“ id”)执行“常规”投影查询,就我所知,该属性应与数据存储键相同datastore.key[kind, record.id] ),因此我正确地检索了投影的“ id”属性,因此:

const query = datastore.createQuery(kind)
  .select('id');
datastore.runQuery(query)
.then( (results) => {
    // whatever...
}

Result: 结果:

results[0]: [ 
  { id: 5289385927 },
  { id: 5483575687 },
  { id: 5540575111 },
  { id: 5540622279 },
  // ... and so on
]

So what is wrong with my "keys-only-query"? 那么我的“仅键查询”怎么了? I have done it exactly in the way the documentation describes. 我完全按照文档描述的方式进行了操作。 But I get only empty results. 但是我只得到空结果。

NOTE: I have tested this only in Datastore emulator. 注意: 我仅在数据存储模拟器中对此进行了测试。 Same result in Datastore Emulator as in AppEngine. 数据存储区模拟器中的结果与AppEngine中的结果相同。

The objects are not empty but contain only datastore keys, which are stored under a symbol property: datastore.KEY . 这些对象不是空的,而是仅包含数据存储区键,这些键存储在符号属性datastore.KEY In javascript, symbol properties might not output by default. 在javascript中,默认情况下可能不会输出符号属性。

You can get entity key using symbol datastore.KEY 您可以使用符号datastore.KEY获取实体密钥。

var keys = results.map(function(res) {
     return res[datastore.KEY];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM