简体   繁体   English

DynamoDB查询/扫描仅返回项的子集

[英]DynamoDB query/scan only returns subset of items

I noticed that DynamoDB query/scan only returns documents that contain a subset of the document, just the key columns it appears. 我注意到DynamoDB查询/扫描只返回包含文档子集的文档,只返回它出现的键列。

This means I need to do a separate Batch_Get to get the actual documents referenced by those keys. 这意味着我需要单独使用Batch_Get来获取这些键引用的实际文档。

I am not using a projection expression, and according to the documentation this means the whole item should be returned. 我没有使用投影表达式,根据文档,这意味着应该返回整个项目。 1 1

How do I get query to return the entire document so I don't have to do a separate batch get? 如何获取查询以返回整个文档,以便我不必单独批量获取?

One example bit of code that shows this is below. 下面显示了一个示例代码。 It prints out found documents, yet they contain only the primary key, the secondary key, and the sort key. 它打印出找到的文档,但它们只包含主键,辅助键和排序键。

t1 = db.Table(tname)

q = { 
    'IndexName': 'mysGSI',
    'KeyConditionExpression': "secKey= :val1 AND " \
                              "begins_with(sortKey,:status)",
    'ExpressionAttributeValues': {
        ":val1": 'XXX',
        ":status": 'active-',
    }
}
res = t1.query(**q)

for doc in res['Items']:
    print(json.dumps(doc))

This situation is discussed in the documentation for the Select parameter. Select参数的文档中讨论了这种情况。 You have to read quite a lot to find this, which is not ideal. 你必须阅读相当多的内容才能找到它,这并不理想。

If you query or scan a global secondary index, you can only request attributes that are projected into the index. 如果查询或扫描全局二级索引,则只能请求投影到索引中的属性。 Global secondary index queries cannot fetch attributes from the parent table. 全局二级索引查询无法从父表中获取属性。

Basically: 基本上:

  • If you query the parent table then you get all attributes by default. 如果查询父表,则默认情况下会获取所有属性。

  • If you query an LSI then you get all attributes by default - they're retrieved from the projection in the LSI if all attributes are projected into the index (so that costs nothing extra) or from the base table otherwise (which will cost you more reads). 如果您查询LSI,那么默认情况下您将获得所有属性 - 如果所有属性都投影到索引中,则从LSI中的投影中检索它们(因此不需要额外费用)或者从基表中获取(这会花费更多读取)。

  • If you query or scan a GSI, you can only request attributes that are projected into the index. 如果查询或扫描GSI,则只能请求投影到索引中的属性。 GSI queries cannot fetch attributes from the parent table. GSI查询无法从父表中获取属性。

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

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