简体   繁体   中英

Query values in multiple document using "IN" in gcp datastore

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".

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)
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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