PartiQL查询以返回包含特定字符串的对象列表中的项目

huangapple go评论69阅读模式
英文:

PartiQL Query to return items in a list of objects that contains certain string

问题

I want to retrieve only the short_name that contains the string "BR", I've tried using PartiQL docs but some features are not allowed in DynamoDB.

这部分内容是想要检索包含字符串 "BR" 的 short_name,我尝试使用 PartiQL 文档,但在 DynamoDB 中有一些功能不允许。

英文:

I want to retrieve only the short_name that contains the string "BR", I've tried using PartiQL docs but some features are not allowed in DynamoDB.
The reason is that right now we are using DynamoDB for Geocache wrong and now I need to filter for a specific country.

SELECT googleResult.address_components FROM Geocache

This is the current return for this query:

"address_components": {
                    "L": [
                        {
                            "M": {
                                "long_name": {
                                    "S": "803"
                                },
                                "short_name": {
                                    "S": "803"
                                },
                                "types": {
                                    "L": [
                                        {
                                            "S": "street_number"
                                        }
                                    ]
                                }
                            }
                        },
                        {
                            "M": {
                                "long_name": {
                                    "S": "Rua Sócrates"
                                },
                                "short_name": {
                                    "S": "R. Sócrates"
                                },
                                "types": {
                                    "L": [
                                        {
                                            "S": "route"
                                        }
                                    ]
                                }
                            }
                        },
                        {
                            "M": {
                                "long_name": {
                                    "S": "Jardim Marajoara"
                                },
                                "short_name": {
                                    "S": "Jardim Marajoara"
                                },
                                "types": {
                                    "L": [
                                        {
                                            "S": "political"
                                        },
                                        {
                                            "S": "sublocality"
                                        },
                                        {
                                            "S": "sublocality_level_1"
                                        }
                                    ]
                                }
                            }
                        },
                        {
                            "M": {
                                "long_name": {
                                    "S": "São Paulo"
                                },
                                "short_name": {
                                    "S": "São Paulo"
                                },
                                "types": {
                                    "L": [
                                        {
                                            "S": "administrative_area_level_2"
                                        },
                                        {
                                            "S": "political"
                                        }
                                    ]
                                }
                            }
                        },
                        {
                            "M": {
                                "long_name": {
                                    "S": "São Paulo"
                                },
                                "short_name": {
                                    "S": "SP"
                                },
                                "types": {
                                    "L": [
                                        {
                                            "S": "administrative_area_level_1"
                                        },
                                        {
                                            "S": "political"
                                        }
                                    ]
                                }
                            }
                        },
                        {
                            "M": {
                                "long_name": {
                                    "S": "Brasil"
                                },
                                "short_name": {
                                    "S": "BR"
                                },
                                "types": {
                                    "L": [
                                        {
                                            "S": "country"
                                        },
                                        {
                                            "S": "political"
                                        }
                                    ]
                                }
                            }
                        },
                        {
                            "M": {
                                "long_name": {
                                    "S": "04671-205"
                                },
                                "short_name": {
                                    "S": "04671-205"
                                },
                                "types": {
                                    "L": [
                                        {
                                            "S": "postal_code"
                                        }
                                    ]
                                }
                            }
                        }
                    ]
                }

答案1

得分: 1

无法在DynamoDB中执行此操作,因为您的数据存储在一个列表中,无法搜索列表中的每个组件以查找特定值。为什么不将这些值存储为一个Map,其中short_name代码是其键?

现在,您可以执行一个Scan,以查找所有具有"BR"作为short_name的项目:

aws dynamodb scan \
--table-name tableName \
--filter-expression 'attribute_exists(#v.#x)' \
--expression-attribute-names '{"#v": "address_components", "#x": "BR"}'
英文:

You cannot do that in DynamoDB, as you have the data stored in a List and you are not able to search every component in a list for a given value. Why not store the values as a Map with the short_name code as its key?

"address_components": {
  "M": {
   "803": {
    "M": {
     "long_name": {
      "S": "803"
     },
     "short_name": {
      "S": "803"
     },
     "types": {
      "L": [
       {
        "S": "street_number"
       }
      ]
     }
    }
   },
   "BR": {
    "M": {
     "long_name": {
      "S": "Brasil"
     },
     "short_name": {
      "S": "BR"
     },
     "types": {
      "L": [
       {
        "S": "country"
       },
       {
        "S": "political"
       }
      ]
     }
    }
   }
  }
 }

Now you can do a Scan for all items which have a short_name of "BR":

aws dynamodb scan \
--table-name tableName \
--filter-expression 'attribute_exists(#v.#x)' \
--expression-attribute-names '{"#v": "address_components", "#x": "BR"}'

huangapple
  • 本文由 发表于 2023年4月13日 20:28:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定