Gianmarco
Gianmarco

Reputation: 832

DynamoDB getItem just with the id (aws sdk)

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);
  }
});

});

Upvotes: 1

Views: 5972

Answers (2)

not-a-robot
not-a-robot

Reputation: 321

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 composite 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.

Upvotes: 0

Nadav Har'El
Nadav Har'El

Reputation: 13711

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.

Upvotes: 5

Related Questions