Google Datastore 组合(联合)多组实体结果以达到 OR 条件 [英] Google Datastore combine (union) multiple sets of entity results to achieve OR condition

查看:23
本文介绍了Google Datastore 组合(联合)多组实体结果以达到 OR 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Datastore 数据库在 Google App Engine 上使用 NodeJS.

I am working with NodeJS on Google App Engine with the Datastore database.

由于 Datastore 没有支持 OR 运算符,我需要运行多个查询并合并结果.

Due to the fact that Datastore does not have support the OR operator, I need to run multiple queries and combine the results.

我计划运行多个查询,然后将结果合并到一个实体对象数组中.我已经有一个查询在工作.

I am planning to run multiple queries and then combine the results into a single array of entity objects. I have a single query working already.

问题:将 Datastore 返回的两组(或更多)实体(包括重复数据删除)组合起来的合理有效方法是什么?我相信这将是集合论中的联合"操作.

Question: What is a reasonably efficient way to combine two (or more) sets of entities returned by Datastore including de-duplication? I believe this would be a "union" operation in terms of set theory.

这是基本查询大纲,将使用一些不同的过滤器多次运行以实现所需的 OR 条件.

Here is the basic query outline that will be run multiple times with some varying filters to achieve the OR conditions required.

  //Set requester username
  const requester = req.user.userName;
  //Create datastore query on Transfer Request kind table
  const task_history = datastore.createQuery('Task');
  //Set query conditions
  task_history.filter('requester', requester);
  //Run datastore query
  datastore.runQuery(task_history, function(err, entities) {
    if(err) {
      console.log('Task History JSON unable to return data results. Error message: ', err);
      return;
      //If query works and returns any entities
    } else if (entities[0]) {
      //Else if query works but does not return any entities return empty JSON response
      res.json(entities); //HOW TO COMBINE (UNION) MULTIPLE SETS OF ENTITIES EFFICIENTLY?
      return;
    }
  });

这是我的原始帖子:具有 OR 条件的 Google 数据存储过滤器

推荐答案

恕我直言,最有效的方法是在第一阶段使用 Keys-only 查询,然后将获得的键组合成一个列表(包括重复数据删除)),然后简单地通过键查找来获取实体.来自投影查询:

IMHO the most efficient way would be to use Keys-only queries in the 1st stage, then perform the combination of the keys obtained into a single list (including deduplication), followed by obtaining the entities simply by key lookup. From Projection queries:

仅键查询

仅键查询(这是一种投影查询)仅返回结果实体的键而不是实体本身,在与检索整个实体相比,延迟和成本更低.

A keys-only query (which is a type of projection query) returns just the keys of the result entities instead of the entities themselves, at lower latency and cost than retrieving entire entities.

先做一个keys-only查询通常更经济,然后从结果中获取实体的子集,而不是执行一般查询可能会获取比您实际需要的更多的实体.

It is often more economical to do a keys-only query first, and then fetch a subset of entities from the results, rather than executing a general query which may fetch more entities than you actually need.

以下是创建仅键查询的方法:

Here's how to create a keys-only query:

const query = datastore.createQuery()
  .select('__key__')
  .limit(1);

此方法解决了您在尝试直接组合通过常规非键查询获得的实体列表时可能遇到的几个问题:

This method addresses several problems you may encounter when trying to directly combine lists of entities obtained through regular, non-keys-only queries:

  • 您无法正确去重,因为您无法区分具有相同值的不同实体和出现在乘法查询结果中的相同实体
  • 通过属性值比较实体可能会很棘手,而且与仅比较实体键相比,肯定会更慢/计算成本更高
  • 如果您无法在单个请求中处理所有结果,那么您会因在不实际使用它们的情况下读取它们而产生不必要的数据存储成本
  • 在仅处理实体键时,将实体的处理拆分为多个请求(例如,通过任务队列)要简单得多

也有一些缺点:

  • 它可能会慢一点,因为您要访问数据存储两次:一次是获取键,一次是获取实际实体
  • 您无法利用仅通过非键的投影查询获取所需的属性

这篇关于Google Datastore 组合(联合)多组实体结果以达到 OR 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆