javascript - AWS Dynamodb 查询 - 从有条件的表中获取项目

标签 javascript node.js database amazon-dynamodb aws-sdk

Dynamo 表包含字段:

  • 电子邮件(主要)
  • 租户
  • 其他东西

我想获取电子邮件中包含“mike”的所有项目

在我的nodejs服务器中,我有这段代码

 const TableName= 'UserTable';
 const db = new aws.DynamoDB();
 const email = 'mike.green@abc.com'

    params = {
      TableName: userTableName,
      KeyConditionExpression: '#email = :email',
      ExpressionAttributeNames: {
        '#email': 'email',
      },
      ExpressionAttributeValues: {
        ':email': { S: email },
      },
    };

  db.query(params, (err, data) => {
    if (err) {
      reject(err);
    } else {
      const processedItems = [...data.Items].sort((a, b) => a.email < b.email ? -1 : 1);
      const processedData = { ...data, Items: processedItems };
      resolve(processedData);
    }

仅当我搜索整个电子邮件 mike.green@abc.com

时,此功能才有效 ^^

问题 1 - 但是,如果我想搜索mike,并返回电子邮件中包含mike的所有项目,我怎样才能得到它?

问题 2 如果我想获取电子邮件包含 mike 且租户为 加拿大 的所有行。我怎样才能得到它?

最佳答案

我不是 NodeJS 用户,但希望它能有所帮助。

Question 1 - But, if i want to search mike, and return all items where email contains mike, How can i get that?

键表达式保留为等式约束。如果你想有更多的查询灵 active ,你需要使用过滤表达式。请注意,您将无法在分区键上使用过滤表达式。您可以在 https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html 上找到更多信息但最重要的是:

Key Condition Expression

To specify the search criteria, you use a key condition expression—a string that determines the items to be read from the table or index.

You must specify the partition key name and value as an equality condition.

You can optionally provide a second condition for the sort key (if present). The sort key condition must use one of the following comparison operators:

a = b — true if the attribute a is equal to the value b

a < b — true if a is less than b

a <= b — true if a is less than or equal to b

a > b — true if a is greater than b

a >= b — true if a is greater than or equal to b

a BETWEEN b AND c — true if a is greater than or equal to b, and less than or equal to c.

The following function is also supported:

begins_with (a, substr)— true if the value of attribute a begins with a particular substring.

……

Question 2 If I want to get all the rows where email contains mike and tenant is Canada. How can i get that?

您可以使用过滤表达式来执行此操作,并使用可用函数之一 https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Syntax 。过滤器表达式为:

If you need to further refine the Query results, you can optionally provide a filter expression. A filter expression determines which items within the Query results should be returned to you. All of the other results are discarded.

A filter expression is applied after a Query finishes, but before the results are returned. Therefore, a Query will consume the same amount of read capacity, regardless of whether a filter expression is present.

A Query operation can retrieve a maximum of 1 MB of data. This limit applies before the filter expression is evaluated.

A filter expression cannot contain partition key or sort key attributes. You need to specify those attributes in the key condition expression, not the filter expression. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html

总结:

  • 如果电子邮件是您的分区键,则无法对其应用 contains - 您必须直接查询它。
  • 最终您可以对表进行扫描并对其应用过滤器( https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html ),但由于表的容量和响应时间的消耗,我不会这样做。扫描涉及对表中的所有行进行操作,因此如果您有数百 GB,您可能无法实时获取信息。实时服务是 DynamoDB 的目的之一。

关于javascript - AWS Dynamodb 查询 - 从有条件的表中获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56873008/

相关文章:

javascript - json 到 javascript 变量 zendesk

python - 从 ElectronJS 脚本运行 python-shell

javascript - Node/Angular 应用程序未捕获语法错误 : Unexpected token <

mysql - 如何构建即时搜索引擎? (具有排名/相关性)

mysql - 单个数据库字段,具有多个引用,MySQL

javascript - 为什么exportRoot不使用createjs和animate cc将movieclip放置在 Canvas 上?

javascript - extjs,在监听器上获取空值而不是空字符串

javascript - 如何创建在 Linux 终端中运行的 Javascript 应用程序?

database - MongoDb 2.6 归档问题

javascript - 加载没有图像的网站以提高效率