简体   繁体   English

在 dynamodb 上进行删除、更新、插入和获取操作嵌套 json 数据结构

[英]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.我想更新,删除房间列表中带有 roomId 的房间。 Should i add a secondary index to roomId?我应该向 roomId 添加二级索引吗? How should be update and delete queries?应该如何更新和删除查询? Also i want to get room informations with roomId.我也想用 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您不能在作为嵌套属性的rooms.roomId上创建Secondary Index

See https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SecondaryIndexes.html请参阅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:为了能够通过roomId进行检索,您可以选择:

  • Use Scan , or使用Scan ,或
  • 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重新设计您的架构以使roomId成为新Table中表的Key的 / 一部分,以便您可以使用实现毫秒Query检索

Example例子

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

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

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