如何按DynamoDB中的数组(或嵌套对象)中的元素进行过滤 [英] How to filter by elements in an array (or nested object) in DynamoDB

查看:483
本文介绍了如何按DynamoDB中的数组(或嵌套对象)中的元素进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据如下:

[
  {
    orgId: "ABC",
    categories: [
      "music",
      "dance"
    ]
  },
  {
    orgId: "XYZ",
    categories: [
      "math",
      "science",
      "art"
    ]
  },
  ...
]

我在 orgId 上有主键,并且我想使用DynamoDB query 来过滤并仅返回类别为科学的项目。

I have the primary key on orgId, and I want to use DynamoDB query to filter and return only items with category "science," for example.

(类别不需要成为任何索引的一部分:只要我可以在Dynamo本身中进行查询,我愿意接受额外的工作人员开销。)

(Category does not need to be part of any index: I am willing to accept the additional worker overhead, provided that I can do the query within Dynamo itself.)

I我有一段时间的狄更斯得到这个工作。我可以很容易地将类别更改为嵌套对象吗?

I am having a dickens of a time getting this working. I can readily change categories into nested objects if that would help?

但是在DynamoDB中比较运算符非常有限似乎没有办法按数组元素或嵌套对象进行过滤?

But the comparison operators are so limited in DynamoDB that it appears there is no way to filter by array elements, or nested objects?

如果没有,这里有什么更好的方法?要将每个类别变成其自己的第一级属性,例如:

If not, what's the better approach here? To turn each category into its own first level attribute, such as:

[
  {
    orgId: "XYZ",
    category_math: true,
    category_science: true
  }
]

肯定不是吗?

推荐答案

 var params = {
  ExpressionAttributeValues: {
   ":orgIdValue": {
     S: "XYZ"
    },
   ":categoriesValue": {
     S: "science"
    }
  }, 
  KeyConditionExpression: "orgId = :orgIdValue", 
  FilterExpression : "categories CONTAINS :categoriesValue", 
  TableName: "MYTABLE"
 };
 dynamodb.query(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
 });




CONTAINS:检查子序列或集合中的值。
AttributeValueList只能包含一个类型为
的String,Number或Binary(不是集合类型)的AttributeValue元素。如果
的目标属性为String类型,则运算符将检查
子字符串是否匹配。如果比较的目标属性为
Binary类型,则运算符将查找目标
与输入匹配的子序列。 如果比较的目标属性是集合
( SS, NS或 BS),则运算符如果发现
与任何值完全匹配,则评估为true集合的成员。
列表支持CONTAINS:评估 a CONTAINS b时, a可以是列表
;但是, b
不能是集合,地图或列表。

CONTAINS : Checks for a subsequence, or value in a set. AttributeValueList can contain only one AttributeValue element of type String, Number, or Binary (not a set type). If the target attribute of the comparison is of type String, then the operator checks for a substring match. If the target attribute of the comparison is of type Binary, then the operator looks for a subsequence of the target that matches the input. If the target attribute of the comparison is a set ("SS", "NS", or "BS"), then the operator evaluates to true if it finds an exact match with any member of the set. CONTAINS is supported for lists: When evaluating "a CONTAINS b", "a" can be a list; however, "b" cannot be a set, a map, or a list.

类别是顶级属性,实际上您没有任何嵌套的属性。顶级标量属性可以建立索引。尽管类别是顶级属性,它不是标量属性(它是一个集合),所以您无法为其编制索引。

Categories is a top level attribute, you don't actually have any nested attributes. Top level scalar attributes can be indexed. Although categories is top level its not a scalar attribute (its a set), so you cannot index it.

您可以使用FilterExpression缩小查询范围,然后可以在列表上使用CONTAINS比较器。

You can use a FilterExpression to narrow down your query, and you can use the CONTAINS comparator on lists.

这篇关于如何按DynamoDB中的数组(或嵌套对象)中的元素进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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