我正在使用祖先查询使用Node.js从Google数据存储中检索实体
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: 'CloSVGoTc350ZXN0cHJvamVjdC0zN2ZiNnI9CxIEdXNlchiAgID409OICgwLEgRzaW1zGICAgICAgIAKDAsSDmNsaWVudFNldHRwsdrfGICAgICA5NEKDBgAIAA=' } ]
从数据存储客户端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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句