简体   繁体   English

如何通过在 Google 数据存储中传递 ID 数组作为输入来检索实体?

[英]How to retrieve entities by passing array of IDs as input in Google datastore?

I am trying to implement the following SQL logic in the datastore,我正在尝试在数据存储中实现以下 SQL 逻辑,

SELECT * from table where id in [1,2,3,4,5]

Implementing this in datastore, I want to retrieve all the corresponding entities with these IDs as an array.在数据存储中实现这一点,我想检索所有具有这些 ID 的相应实体作为数组。

let employees = []
try {
  for (let id of idArray) {
    const employee = await employeeRepo.getOneById(workspace, id)
    employees.push(employee)
  }
} catch (e) {
  throw e;
 }

This is the naive logic of the function, and I am trying to reduce it to a single query.这是 function 的幼稚逻辑,我试图将其简化为单个查询。

Are you using the Node.js library referenced here: https://cloud.google.com/datastore/docs/reference/libraries您是否使用此处引用的 Node.js 库: https://cloud.google.com/datastore/docs/reference/libraries

There is a function get where you can pass in an array of keys and it will return the array of entities.有一个 function get可以在其中传入一个键数组,它将返回实体数组。

https://googleapis.dev/nodejs/datastore/latest/Datastore.html#get https://googleapis.dev/nodejs/datastore/latest/Datastore.html#get

It's possible to do by using get as mentioned in the documentation可以通过使用文档中提到的get来完成

Here's an example of how to do it based on your code:以下是如何根据您的代码执行此操作的示例:

const employee1 = this.datastore.key(['Employee', 1]);
const employee2 = this.datastore.key(['Employee', 2]);
const employee3 = this.datastore.key(['Employee', 3]);

const keys = [employee1, employee2, employee3];

try {
    const [employees] = await datastore.get(keys);
} catch (e) {
    throw e;
}

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

相关问题 如何在Google表格中遍历事件ID以检索附件ID? - How to loop through event IDs in Google Sheets to retrieve attachment Ids? 在Google数据存储区上将实体传输为JSON格式 - Transfer entities to JSON format on Google Datastore 将数组传递给隐藏的输入,并使用不同索引上的元素检索数组 - Passing array to hidden input and retrieve array with elements on different indexes 如何创建具有所有ID的数组 <input type=“checkbox”> 在jQuery中? - How to create an array of all ids having <input type=“checkbox”> in jQuery? 在Google App脚本上将数组输入值传递给code.gs - Passing array input values to code.gs on google app scripting 通过ID数组从mongodb中检索记录 - Retrieve records from mongodb by array of IDs 在输入字段中检索数组 - Retrieve array in input field 如何在浏览器不解释html特殊实体的情况下检索输入的值? - How to retrieve an input's value without the browser interpreting html special entities? 如何通过传递参数从列表数组中检索特定数据作为字符串? - How to retrieve a specific data as a string from an array of list by passing a parameter? Google App Engine数据存储区-将HTML中的键值传递给js函数 - Google App Engine Datastore - key value passing in HTML to js function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM