首页 > 解决方案 > 如何使用 NodeJS 和条件表达式访问 DynamoDB 中的嵌套元素?

问题描述

我在尝试使用条件表达式访问嵌套在 DynamoDB 表中的元素时遇到问题。我正在尝试的代码如下:

const params = {
TableName: 'Games',
Key: {
  id: gameID.id,
},
UpdateExpression: 'SET #players = list_append(#answers , :answer)',
ConditionExpression: '#players CONTAINS :playerID',
ExpressionAttributeNames: {
  '#players': 'Players',
  '#answers': 'answers',
},
ExpressionAttributeValues: {
  ':playerID': userID.id,
  ':answer': tempAnswerList,
},
ReturnValues: 'ALL_NEW',

例如,此版本引发错误:

Invalid ConditionExpression: Syntax error; token: "CONTAINS", near: "#players CONTAINS :playerID"

示例数据库架构

有没有办法在不知道它的 id 的情况下查看数组?就像我在#players 上寻找要使用的通配符一样,因为它是一个列表,但我在文档中看不到。

谢谢,马克

标签: node.jsamazon-web-servicesnosqlamazon-dynamodb

解决方案


请按contains如下所述更改并尝试。

ConditionExpression: 'contains (#players, :playerID)',

推荐阅读