最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - DynamoDB getItem just with the id (aws sdk) - Stack Overflow

programmeradmin0浏览0评论

Is there any way to get an element or more elements (with the same id) with just the id ?, without using alsoe the property name ?

  let params = {
  TableName: "TableName",
  Key: {
    id: { S: req.body.ProjectId },
    // name: { S: req.body.name }
  },
};

ddb.getItem(params, function (err, data) {
  if (err) {
    console.log("Error", err);
    res.send(err);
  } else {
    console.log("Success", data);
    res.send(data);
  }
});

});

Is there any way to get an element or more elements (with the same id) with just the id ?, without using alsoe the property name ?

  let params = {
  TableName: "TableName",
  Key: {
    id: { S: req.body.ProjectId },
    // name: { S: req.body.name }
  },
};

ddb.getItem(params, function (err, data) {
  if (err) {
    console.log("Error", err);
    res.send(err);
  } else {
    console.log("Success", data);
    res.send(data);
  }
});

});

Share Improve this question edited Jul 21, 2020 at 8:49 Gianmarco asked Jul 21, 2020 at 8:19 GianmarcoGianmarco 8422 gold badges16 silver badges40 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You left out some important information in your question, but I am guessing that id is your partition key, and name is the sort key.

In That case the answer is yes - you can get all the items with the same partition key id, by using a Query request instead of the GetItem request.

Please read the documentation of how to properly use Query. In particularly, note that a Query can theoretically return a very long list of items (which have the same id but different name) so it is paged, i.e., you may need to have to call it multiple times (in the appropriate way) to get all these items.

Short answer: You can pull one item depending how the Primary Key of your table has been defined.

Longer version: Here is what API documentation for getItem (here) says about the Key field:

A map of attribute names to AttributeValue objects, representing the primary key of the item to retrieve.

For the primary key, you must provide all of the attributes. For example, with a simple primary key, you only need to provide a value for the partition key. For a posite primary key, you must provide values for both the partition key and the sort key.

GetItem returns ONE record based on the primary key. Primary key uniquely identifies a record in the Dynamo table.

To get multiple records, use BatchGetItem and pass multiple Primary Keys for the records you want to pull.

发布评论

评论列表(0)

  1. 暂无评论