在GCP Datastore中使用“IN”查询多个文档的值。

huangapple go评论70阅读模式
英文:

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)
    

huangapple
  • 本文由 发表于 2023年1月4日 17:07:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75003162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定