简体   繁体   English

如何使用NodeJS删除dynamoDb中的嵌套项目

[英]How to delete a nested item in dynamoDb using NodeJS

I have a table in DynamoDB called 'tasks'. 我在DynamoDB中有一个名为“任务”的表。 This is an example of what a task in 'tasks' looks like: 这是“任务”中的任务的示例:

{
    "taskId": "e3b8d901-6d74-4caa-9360-5b2f7aaec513",
    "notes": [
        {
            "noteId": "aeeeeb60-3221-4d4e-b362-d63b48f42fba",
            "text": "thing to do next and words",
            "status": "TODO"
        },
        {
            "noteId": "88eeeb60-3221-4d4e-b362-d63b48f42fba",
            "text": "other text and stuff",
            "status": "QUESTION"
        }
    ],
    "title": "Some Task"
}

I would like to delete a note from a task. 我想从任务中删除笔记。 To delete a task has been simple- I use the taskId as the key. 删除任务很简单-我使用taskId作为键。 However, I'm not sure how to use the key when accessing a nested item in an array, and I haven't been able to figure it out from the docs. 但是,我不确定在访问数组中的嵌套项时如何使用该键,而且我无法从文档中找出它。

This is what my route looks like: 这是我的路线:

api.delete('/tasks/{id}/notes/{noteId}', (request) => {

  return deleteNote(request.pathParams.id, request.pathParams.noteId)
}, {
  error: 400
})

And this is how I'm trying to delete a note in DynamoDB: 这就是我试图删除DynamoDB中的注释的方法:

function deleteNote(taskId, noteId){

  return docClient.delete({
    TableName: 'tasks',
    Key: {
      noteId: noteId
    }
  }).promise()
    .then((res) => {
      console.log('Note deleted!', res)
      return res
    })
    .catch((error) => {
      console.log('Note not deleted', error);
      throw error
    })
}

Of course, this doesn't work because noteId is not a top-level partition key. 当然,这是行不通的,因为noteId不是顶级分区键。 How can I filter the tasks for the right one, then filter note notes for the right one? 如何过滤正确任务的任务,然后过滤正确任务的笔记? I have an idea of how to do that with JavaScript, but not in DynamoDB. 我对如何使用JavaScript有所了解,但在DynamoDB中却没有。

Thanks! 谢谢!

UPDATE TO ADD: 更新添加:

As Pari has pointed out below, this should really be an update to a task, not a delete. 正如Pari在下面指出的那样,这实际上应该是对任务的更新,而不是删除。 Here's how I modified my 'edit task' to include notes, but I get the error: "The provided expression refers to an attribute that does not exist in the item". 这是我修改“编辑任务”以包含注释的方式,但是出现错误:“提供的表达式引用了该项目中不存在的属性”。

My thinking of how I've done it below is that the whole updated notes array (the array with the one note I want to delete removed) would replace the notes array. 我对以下操作的思考是,整个更新后的notes数组(删除了要删除的一个便笺的数组)将替换notes数组。

function editTask(taskId, updatedTask){
  if (!taskId || !updatedTask) {
    throw new Error('Insufficient infromation to update task')
  }

  return docClient.update({
    TableName: 'tasks',
    Key: {
      taskId: taskId
    },
    UpdateExpression: 'set title = :t, set notes = :n',
    ExpressionAttributeValues: {
      ':t': updatedTask.title,
      ':n': updatedTask.notes
    },
    ReturnValues: 'ALL_NEW'
  }).promise()
    .then((res) => {
      console.log('Task updated!', res)
      return res
    })
    .catch((error) => {
      console.log('Task not updated', error);
      throw error
    })
}

Technically the Notes are not their own table, so deleting them would require you to update the document "Task" without the note you don't want. 从技术上讲,Notes并不是它们自己的表,因此删除它们将需要您在没有您不需要的注释的情况下更新文档“ Task”。 So I would recommend updating the instead of deleting the document. 因此,我建议您更新而不是删除文档。 So perform a get on that Task with that Id as you are doing, and then use the .filter method and remove that element with splice. 因此,请在执行操作时使用该ID对该任务执行get操作,然后使用.filter方法并使用splice删除该元素。 Then perform a .put instead of .delete with that id. 然后执行.put而不是具有该ID的.delete。 Im sure there is a way to do this with params. 我肯定有办法用params做到这一点。

So after looking at it a little more, I saw a couple of java examples achieve this. 因此,多看一点之后,我看到了几个java示例实现了这一点。 Have you tried with update instead of delete? 您是否尝试过使用更新而不是删除?

 api.get('/tasks/{id}/notes/{nodeId)',(req,res)=>{ let id = req.params.id; let noteId = req.params.noteId; updateTask(id, noteId); }) function updateTask(id, noteId){ return docClient.update({ TableName: 'tasks', Key: { noteId: noteId } }).promise() .then((res) => { console.log('Note deleted!', res) return res }) .catch((error) => { console.log('Note not deleted', error); throw error }) } 

Please Update your UpdateExpression with : 请使用以下命令更新您的UpdateExpression

UpdateExpression: 'SET title = :t, notes = :n' , UpdateExpression: 'SET title = :t, notes = :n'

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

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