我正在尝试获取 dynamodb 列表中的特定项目

问题描述 投票:0回答:1

我正在尝试从 dynambodb 列表中获取特定项目,下面是表架构

{
  "UserId": {
    "N": "0"
  },
  "Messages": {
    "L": [
      {
        "M": {
          "absoluteTime": {
            "S": "638430874210000000"
          },
          "friendId": {
            "N": "1"
          },
          "messageBody": {
            "S": "Message 1"
          },
          "messageDate": {
            "S": "10/2/2024"
          },
          "messageTime": {
            "S": "20:20"
          }
        }
      },
      {
        "M": {
          "absoluteTime": {
            "S": "638430874210000000"
          },
          "friendId": {
            "N": "2"
          },
          "messageBody": {
            "S": "Message 2"
          },
          "messageDate": {
            "S": "10/2/2024"
          },
          "messageTime": {
            "S": "20:20"
          }
        }
      }
    ]
  }
}

我试图获取

Messages
的列表,其中
friendId
等于 1。 这是我的
queryRequest

          var queryRequest = new QueryRequest
          {
              TableName = tableName,
              KeyConditionExpression = "UserId = :userId",
              ExpressionAttributeValues = new Dictionary<string, AttributeValue>
              {
                  { ":userId", new AttributeValue { N = userId } },
                  { ":friendId", new AttributeValue { N = "1" } } 
              },
              FilterExpression = "contains(Messages.friendId, :friendId)", 
              ProjectionExpression = "Messages"
          };

它不会返回任何内容,如果我只是在

Messages
上查询,我就可以获得所有
userId
的列表,并且使用循环我可以将它们过滤掉。但有没有办法只获取
Messages
的列表,其中
friendId
为 1? 这就是我想要的输出。

  "Messages": {
    "L": [
      {
        "M": {
          "absoluteTime": {
            "S": "638430874210000000"
          },
          "friendId": {
            "N": "1"
          },
          "messageBody": {
            "S": "Message 1"
          },
          "messageDate": {
            "S": "10/2/2024"
          },
          "messageTime": {
            "S": "20:20"
          }
        }
      }
   ]
}
asp.net-core amazon-dynamodb
1个回答
0
投票

您想要做的事情是不可能的,除非您知道存储的地图的全部内容,然后您可以使用 contains。

在大多数情况下,最好存储嵌套映射,因为它在查询或更新嵌套对象时为您提供更大的灵活性。

对于列表,您必须知道对象的位置或对象的所有内容。

© www.soinside.com 2019 - 2024. All rights reserved.