简体   繁体   English

如何通过 Google 数据存储中的嵌套键进行查询?

[英]How to query through nested keys in Google datastore?

I have users, stored under specific unique IDs.我有用户,存储在特定的唯一 ID 下。 I need to query them by a certain property, but I cannot figure out how to do that when my unique ID is part of the key.我需要通过某个属性来查询它们,但是当我的唯一 ID 是密钥的一部分时,我无法弄清楚如何做到这一点。

const user1 = {
    id: 1,
    name: 'John Smith',
    cityId: 102,
};

const user1 = {
    id: 2,
    name: 'Rudy Black',
    cityId: 102,
};

const upsertUser = user => {
    const key = datastore.key([ 'users', user.id ]);

    return datastore.upsert({ key, data: user });
});

const getUsersInCity = async cityId => {
    const query = datastore.createQuery('users').filter('cityId', '=', cityId);

    const [ users ] = await datastore.runQuery(query);

    return users;
};

upsertUser(user1);
upsertUser(user2);

console.log(await getUsersInCity(102)); // Expected: John Smith, Rudy Black

An example of code that you can give it a try using to query values via ID, it's using the below code:您可以尝试使用通过 ID 查询值的代码示例,它使用以下代码:

const key = this.datastore.key(['Customer', id:number]);
await this.datastore
  .get(key)
  .then(results => {
    console.log(results);
  })
  .catch(err => { console.error('ERROR:', err); });

This code was get from the article Google DataStore Query By Nodejs .此代码来自文章Google DataStore Query By Nodejs This article provides some good examples of queries that you can give a look.本文提供了一些很好的查询示例,您可以查看一下。

I would recommend you to take a look at it and use the above code as a start point.我建议您看一下它并使用上面的代码作为起点。

Let me know if the information helped you!如果这些信息对您有帮助,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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