[英]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.