如何从谷歌数据存储中祖先查询的返回对象中检索完整键?

Aka*_*han 5 google-app-engine node.js google-cloud-datastore

我正在使用祖先查询使用 nodejs 从谷歌数据存储中检索实体

query = datastore.createQuery(entity).hasAncestor(key)

关键在哪里

key = datastore.key([kind_name_of_parent, id_of_parent])

我能够检索对象,但我想获取检索对象的完整键,而返回的数组仅包含返回的对象和 endCursor。

我怎样才能得到完整的密钥?或者,我可以从 endCursor 获得完整的密钥吗?

我的查询结果的一个例子是:

[{ modTS: 1481006473081, modLoc: null, modUid: 0, createTS: 1481006473081 } ], { moreResults: 'NO_MORE_RESULTS', endCursor: 'CloSVGoTc350ZXN0cHJvamVjdC0zN2ZiNnI9CxIEdXNlchiAgID409OICgw??LEgRzaW1zGICAgICAgIA??KDAsSDmNsaWVudFNldHR??wsdrfGICAgICA5NEKDBg??AIAA=' } ]

Fra*_*son 4

从数据存储客户端v0.42.2开始,现在使用数据存储客户端上的符号来引用密钥datastoreClient.KEY

在 CLI 上运行此命令,如果第一次不起作用,请再次运行它(由于“最终一致性”,第一次可能会失败)。

'use strict';

const Datastore = require('@google-cloud/datastore'),
    projectId = 'your-project-id',
    datastore = Datastore({
        projectId: projectId
    }),
    pkind = 'Foo',
    pname = 'foo',
    kind = 'Bar',
    name = 'bar',
    parentKey = datastore.key([pkind, pname ]),
    entityKey = datastore.key([pkind, pname, kind, name]),
    entity = {
        key: entityKey,
        data: {
            propa: 'valuea'
        }
    },
    query = datastore.createQuery().hasAncestor(parentKey).limit(5);

let complete = false;

datastore.save(entity).then(() => {
    datastore.runQuery(query).then((res) => {
        try {
            console.log('parent key ', res[0][0][datastore.KEY].parent);
        } finally {
            complete = true;
        }
    });

});

function waitUntilComplete() {
    if (!complete)
        setTimeout(waitUntilComplete, 1000);
}

waitUntilComplete();
Run Code Online (Sandbox Code Playgroud)