简体   繁体   中英

Making delete, update, insert and get operations nested json data structure on dynamodb

My data structure is like below: 在此处输入图像描述

I want update,delete a room in the rooms list with roomId. Should i add a secondary index to roomId? How should be update and delete queries? Also i want to get room informations with roomId.

For remove a room, can the code structure be like that:

  const roomItem = {
    TableName: 'Home',
    Key: {
      homeId: homeId
    },
    IndexName: 'roomId-index',
    ConditionExpression: 'rooms.roomId = :roomId',
    ExpressionAttributeValues: {
      ':roomId': roomId
    }
  };
  await dynamodb.delete(roomItem).promise();

Creating a Secondary Index on a non top-level Attribute

You cannot create a Secondary Index on rooms.roomId where it is a nested attribute

See https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SecondaryIndexes.html

Every attribute in the index key schema must be a top-level attribute of type String, Number, or Binary.

--

To be able to retrieve by roomId , you have the options of either:

  • Use Scan , or
  • Re-design your schema to make roomId a / part of a Table's Key in a new Table so that you can use achieve milliseconds Query retrieval

Example

Hash Key (House Id)  | Range Key (Room Id)
---------------------+---------------------
55555555             | 1
---------------------+---------------------
55555555             | 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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