英文:
Query values in multiple document using "IN" in gcp datastore
问题
在GCP数据存储中,我有以下文档:
Kind: "User"
{name: "jay", age: 24, key: Key(["User", 1])}
{name: "jet", age: 35, key: Key(["User", 2])}
{name: "sam", age: 28, key: Key(["User", 3])}
我想要使用GCP数据存储的Node SDK查询具有以下键的用户:Key(["User", 1])
和 Key(["User", 2])
。
我正在使用以下查询来实现这个目标:
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const key1 = datastore.key(["User", 1]);
const key2 = datastore.key(["User", 2]);
const query = datastore.createQuery("User").filter('__key__', 'IN', [key1, key2]);
但是,在运行上面的查询时,我收到以下错误信息:
InvalidKey: A key should contain at least a kind.
但是,这些键已经包含了"User"种类(kind)信息。
英文:
I have following documents in gcp datastore:
Kind: "User"
{name: "jay", age: 24, key: Key(["User", 1])}
{name: "jet", age: 35, key: Key(["User", 2])}
{name: "sam", age: 28, key: Key(["User", 3])}
Using node sdk for gcp datastore I want to query users with keys: Key(["User", 1]) & Key(["User", 2])
I am using following query for the same:
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const key1 = datastore.key(["User", 1]);
const key2 = datastore.key(["User", 2]);
const query = datastore.createQuery("User").filter('__key__', 'IN', [key1, key2]);
On running the above query using following code:
datastore.runQuery(query)
.then(users => {
console.log(users)
})
.catch(err => console.log(err));
I am getting following error:
InvalidKey: A key should contain at least a kind.
But the keys already contain the kind "User".
答案1
得分: 1
根据文档(查找"您可以查找多个实体"),您应该尝试这样做:
const [users] = await datastore.get([key1, key2]);
console.log(users)
英文:
According to the documentation (look for 'You can look up multiple entities'), you should try this instead
const [users] = await datastore.get([key1, key2]);
console.log(users)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论