DynamoDB nested query support

后端 未结 4 1855
逝去的感伤
逝去的感伤 2020-12-15 18:52

Do Amazon DynamoDB scan operation allow you to query on nested attributes of type Array or Object? For example,

{
    Id: 206,
             


        
相关标签:
4条回答
  • 2020-12-15 19:18

    Yes, you can query on nested attributes of type array or object using scan or query .

    Reference for Python boto3: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html#querying-and-scanning

    Example: Suppose you want to find out records for which the RearView" > 500 and second item of RelatedItems" > 200, you can do the following:

    data = table.scan(
        FilterExpression=Attr('RelatedItems[1]').gt('200') & Attr('Pictures.RearView').gt('500'))
    
    0 讨论(0)
  • Please note that in DyanomoDB query and scan are quite different (scan is a much costlier operation). So while you can filter on both as pointed out by @coffeeplease; you can only query/index on:

    The key schema for the index. Every attribute in the index key schema must be a top-level attribute of type String, Number, or Binary. Other data types, including documents and sets, are not allowed (ref).

    0 讨论(0)
  • 2020-12-15 19:30

    Yes, you can by passing list or value.

    data = table.scan(FilterExpression=Attr('RelatedItems').contains([1, 2, 3]) & Attr('Pictures.RearView').eq('1'))
    
    0 讨论(0)
  • 2020-12-15 19:35

    Yes, you can use a Filter Expression, which is just like Condition Expression. The section that talks about the functions that you can use in these types of expressions mentions the following:

    "For a nested attribute, you must provide its full path; for more information, see Document Paths."

    The Document Paths reference has examples on how to reference nested attributes in DynamoDB data types like List (what you are calling an array) and Map (what you are calling an object). Check out that reference for examples on how to do so:

    • MyList[0]
    • AnotherList[12]
    • ThisList[5][11]
    • MyMap.nestedField
    • MyMap.nestedField.deeplyNestedField
    0 讨论(0)
提交回复
热议问题