简体   繁体   English

如何从DynamoDB过滤(扫描)映射列表

[英]How to filter (Scan) mapped lists from DynamoDB

This is my db structure 这是我的数据库结构

{
  "Accounts": [
   {
     "accountId": "12345",
     "region": "us-east-1"
   }
  ],
  "createdBy": "abcd@gmail.com",
}

I have several rows of similar structure. 我有几行类似的结构。 I need to filter the accounts based on the regions. 我需要根据地区过滤帐户。 Can anyone help me with the filterExpression for the scan? 谁能帮我使用filterExpression进行扫描? Any help would be appreciated. 任何帮助,将不胜感激。

{
      {
         "Accounts": [
             {
                 "accountId": "12345",
                 "region": "us-east-1"
             },
          ],
          "createdBy": "abcd@gmail.com",
      },
      {
         "Accounts": [
             {
                 "accountId": "98765",
                 "region": "us-west-1"
             },
             {
                 "accountId": "45678",
                 "region": "eu-west-2"
              }
          ],
          "createdBy": "pqrs@gmail.com",
      },
}

I assume this is your table's data when two items (just an assumption, inform me if I am wrong) are present in your table. 当表中存在两项(假设,如果我错了,请告诉我)时,我假设这是您表的数据。 First thing is that if your table contains items like this and you need to filter out "Accounts" from each item which are in a particular region, then it is not possible because you are asking for only a part of an Item and in reality when you scan a table using filterExpressions, it doesn't return you a part of an Item, it will return you a whole Item depending on you filter-expression. 第一件事是,如果您的表中包含类似这样的项目,并且您需要从特定区域中的每个项目中过滤掉“帐户”,那么这是不可能的,因为您只需要一个项目的一部分,而实际上您使用filterExpressions扫描表格时,它不会返回Item的一部分,而是会根据您的filter-expression返回整个Item。 Still I will try to explain how you can make it work. 我仍然会尝试解释如何使其工作。

As each item from your table contains a "Accounts" key which has a list as value. 由于表中的每个项目都包含一个“ Accounts”键,该键具有一个列表作为值。 You can do use a contains filterAttribute expression, but then that is only for lists containing string values, something like below 您可以使用一个contains filterAttribute表达式,但这仅适用于包含字符串值的列表,如下所示

{
    "movie": "XYZ",
    "country": "India",
    "information": {
        "actors": [
            "ABC"
        ],
        "genres": [
            "Action"
            "Romance",
            "Drama",
        ]
    }
}

For this you can use something like 为此,您可以使用类似

response = table.scan(
    FilterExpression=Attr('information.genres').eq('Romance')
)

But as in your case the list contains more objects, it is not directly possible to scan using any filter expression. 但是,由于您的情况列表包含更多对象,因此无法直接使用任何过滤器表达式进行扫描。 You have to restructure your database in a fashion that you can apply filter to scan or you can scan the whole table in a single object and then use your programming language's features to filter out the data from your data object. 您必须以一种可以应用过滤器进行扫描的方式来重组数据库,或者可以在单个对象中扫描整个表,然后使用编程语言的功能从数据对象中过滤出数据。

For restructuring I would suggest you a small change which will be helpful, as your "Accounts" key is a list having objects which have two fields "accountId" and "region". 对于重组,我建议您进行一些小的更改,这将有所帮助,因为您的“帐户”键是一个包含对象的列表,这些对象具有两个字段“ accountId”和“ region”。 You can change type of "Accounts" key to an object and inside that object maintain two lists, one for "accountId"s and one for "regions"s, Something like below: 您可以将“ Accounts”键的类型更改为一个对象,并且在该对象内维护两个列表,一个用于“ accountId”,一个用于“ regions”,如下所示:

{
      "Accounts": {
         "accountId": ["12345", "23456", "98765"],
         "region": ["us-east-1", "us-west-1", "eu-west-1"]
      },
      "createdBy": "abcd@gmail.com",
 }

Now you have two lists, so for accountId[0], your region will be present in region[0] like that. 现在您有两个列表,因此对于accountId [0],您的区域将以这种方式出现在region [0]中。 Now you can scan using filter expression like below, 现在,您可以使用以下过滤器表达式进行扫描,

response = table.scan(
        FilterExpression=Attr('Accounts.region').contains('us-west-1')
    )

So this will return you the items which have atleast an account in us-west-1 but still you need to filter it out for getting only the accounts which are in "us-west-1" 因此,这将返回您在us-west-1中至少有一个帐户的项目,但仍然需要过滤掉它以仅获取“ us-west-1”中的帐户

Hope it helps. 希望能帮助到你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM