【问题标题】:How to search for an object in a list inside an object DynamoDB and Python如何在对象 DynamoDB 和 Python 内的列表中搜索对象
【发布时间】:2018-10-12 11:18:15
【问题描述】:

我需要一些帮助才能从 DynamoDB 中的嵌套 JSONschema 中获取项目。我会向你解释架构,你可以告诉我是否可能。

架构是:

{
    "updated_at": "2018/05/02 08:32:10",
    "created_at": "2018/05/02 08:32:10",
    "updated_by": "igor",
    "created_by": "igor",
    "application": [
        {
            "name": "driver app",
            "features": [
                {
                    "name": "passenger list",
                    "settings": [],
                    "description": "feature for passenger list",
                    "id": 2
                }
            ],
            "id": 1,
            "url": "play store"
        },
        {
            "name": "passsenger app",
            "features": [],
            "id": 2,
            "url": "play store"
        }
    ],
    "address": "New York",
    "id": 4,
    "url": "https://airlink.moovex.com",
    "name": "airlink",
    "service_locations": [
        {
            "title": "IL",
            "latitude": 32,
            "longitude": 35
        }
    ]
}

我需要通过查询从我的应用程序列表中获取对象。

【问题讨论】:

  • 不幸的是,这在 dynamodb 上是不可能的。 CONTAINS 函数要求对象中的所有属性与列表中的项目相匹配。比如需要id、url、features等。

标签: python-3.x amazon-web-services aws-lambda amazon-dynamodb


【解决方案1】:

我不确定这是否能回答您的问题,但是,如果您需要检索具有特定 ID 的对象的应用程序列表,您会这样做

import boto3

DyDB = boto3.resource('dynamodb')

table = DyDB.Table('YourTableName')

response = table.query(
    ProjectionExpression='application',
    KeyConditionExpression=Key('id').eq(4) # where 4 is the id you need to query
)

# this is just to test the result
for app in response['Items'][0]['application']:
    print(app['id'])

响应将返回(在列表项中),应用程序属性及其内部的应用程序列表

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 2011-07-08
    相关资源
    最近更新 更多