2016-11-30 10 views
1

私はDynamoDBテーブルにアイテムを持っています。項目は次のようになります。DynamoDB - アイテムを読み取り、配列サイズを返す

{ 
    data: [ 1, 2, 3, 4, 5, 6 ] 
    more_data: [ 2, 3, 4, 5, 6, 7 ] 
} 

ProjectionExpressionを使用して、私はGetItemを行うだけdataを返す、またはdata[0]を返すことができます。しかし、私は理解できない2つのことがあります:

  • どのように私は配列のサイズを返すことができますか? data.SIZE
  • 配列のサブセットを返すにはどうすればよいですか? data[2-10]

ProjectionExpressionのドキュメントではこれをカバーしていないようです。すべての手がかりは?

答えて

1

1)DynamoDBには、List属性のサイズを取得する機能がありません。配列要素のサイズは、length関数を使用してクライアント側で見つけることができます。

data.Item.records.lengthは、配列要素recordsの長さを示します。 recordsはDynamoDBテーブルのリストです。

docClient.get(params, function(err, data) { 
    if (err) { 
     console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2)); 
    } else { 
     console.log("GetItem succeeded:", JSON.stringify(data, null, 2)); 
     console.log(data.Item[0].records.length); 
    } 
}); 

2)サブアレイを登録すると、構文は次のようになります。 DynamoDBは'records[1-3]'のような範囲をサポートしていません。

ProjectionExpression: 'records[0], records[1]', 

3)DynamoDBには、リスト内の最後の項目を取得する機能がありません。しかし、あなたはクライアント側でそれを得ることができます。私はrecordsのリストを持っています。私は最後の要素を持っていますitemValue.records[itemValue.records.length - 1]

docClient.query(params, function(err, data) { 
    if (err) { 
     console.error("Unable to read item. Error JSON:", JSON.stringify(err, 
       null, 2)); 
    } else { 
     console.log("GetItem succeeded:", JSON.stringify(data, null, 2)); 

     data.Items.forEach(function(itemValue) { 
      console.log (itemValue.records[itemValue.records.length - 1]); 
     });  
    } 
}); 
+0

ありがとう、それは役に立ちます。配列の最後の項目を返す投影式はありますか? – Lucio

関連する問題